/*
 *  myangio.cpp
 *  fix
 *
 *  Created by wumin on 8/2/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *should i reverse the list???
 Yes, do it
 DON'T FORGET TO SET SPROUTTIP BACK TO 0
 */
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

#include "../ImprovedMath.h"
#include "../random.h"
#include "../ParseText.h"
//#include "myangio.h"

#include "../Plotting.h"

#include "../Matrix.h"
#include "../UCI_TumorGrowth/Microenvironment.h"
#include "../DiffusionSolver.h"


#include "../UCI_TumorGrowth/GhostFluidMethod.h"
#include "../UCI_TumorGrowth/Curvature.h"
#include "../UCI_TumorGrowth/GaussianFilter.h"
#include "../UCI_TumorGrowth/DiffusionSolverSteadyState.h"
#include "../UCI_TumorGrowth/AdvectTumor.h"
#include "../UCI_TumorGrowth/NecrosisLevelSets.h"
#include "../LogfileEntry.h"
#include "../UCI_TumorGrowth/Oxygen.h"

#include "../AngiogenesisInterface.h"

#include "../ParseText.h"

#include "../UCI_TumorGrowth/Pressure.h"


extern double AngioDeltaT;
double AngioDeltaY;
double AngioDeltaX;
double FlowDeltaT;
double MaxTAF;
double MaxWss;
bool VesselBroke;
int touchpoints;

extern bool FlowBegin;
extern bool AdaptionBegin;
extern char* ParameterFile;
Matrix Angio_Iteration_Change_Matrix;


VesselCell::VesselCell()
{ 
 coverlimit = 15;
 cConnectRange = (coverlimit+1)*2;
 cIndex = 0;
 cSetPressure = 0;
 cCover = 0;
 cConnection = 0;
 cFate = 'S';
 cReceptor = 0;
 cRadius = 1;
 cPermeability = 0.0;
 cPericytes = 0;
 //cConnectIndex = zeros(1,6);
 
 
 pNextVesselCell = NULL;
 pPreviousVesselCell = NULL;
} 



VesselCell::~VesselCell()
{
cout<<"One Cell died";
}





CellVesselStructure::CellVesselStructure()
{
	pFirstVesselCell = NULL;
	pLastVesselCell = NULL;
	SproutNumber = 0;
	
	CellListLength = 0;
	//cout <<"function VesselStructure running"<<endl;	
}

CellVesselStructure::~CellVesselStructure()
{
//cout <<"function ~VesselStructure running"<<endl;	
 VesselCell* pCurrentVesselCell = pFirstVesselCell;
 if( pCurrentVesselCell)
 {
	while(pCurrentVesselCell)
	{
		pCurrentVesselCell = pCurrentVesselCell ->pNextVesselCell;
		delete  pCurrentVesselCell->pPreviousVesselCell;
	}
	delete  pCurrentVesselCell;
 }
}





void CellVesselStructure::InitializeCellTime(bool Resume)
{
 
//cout <<"function Initializetime running"<<endl;
 extern double t;
 FlowBegin = (bool) RetrieveDoubleParameter( ParameterFile , "FlowBegin");
 AdaptionBegin = (bool) RetrieveDoubleParameter( ParameterFile , "AdaptionBegin");
 if(t>0)
 {
	 FlowBegin=1;
	 AdaptionBegin=1;
 }
 if( Resume == 1)
 {
 /*
 extern char SaveDirectory[1025];
 char temp_string[1024];
 
	 sprintf( temp_string , "%s/angio_resume.dat" , SaveDirectory );
 ifstream angio_resume( temp_string, ios::in );
 angio_resume >> AngioDeltaT;
			 
 cout<<AngioDeltaT<<"   "<<endl;
            
 angio_resume.close();
 */
 }
 else
 {
	AngioDeltaT = 0.05; //THIS IS WHERE I'M NOT SURE OF
	cout<<"AngioDeltaT"<<AngioDeltaT<<endl;
	

	
 }
//cout <<"function Initializetime finished"<<endl; 
}	




void CellVesselStructure::InitializeCellList(bool VesselResume)
{	
	
	
	extern int Xnodes;
	extern int Ynodes;
	//Indicator = zeros(Xnodes, Ynodes);
	extern bool AngiogenesisIsStarted;
	pFirstVesselCell = new VesselCell;
	pFirstVesselCell->pPreviousVesselCell = NULL;
	
	if (VesselResume == 0 && !AngiogenesisIsStarted)
	{
		extern int Xnodes;
		extern int Ynodes;
		int InitialListLength = 40;
		
		//int InitialSprout = 5;
		
		VesselCell* pCurrentVesselCell = pFirstVesselCell;
		
		/****empty point*******/	
		
		pCurrentVesselCell->cIndex = 0;
		pCurrentVesselCell->i[0] =-1;
		pCurrentVesselCell->j[0] = -1;
		
		pCurrentVesselCell->cConnection = 0;
		pCurrentVesselCell->cSetPressure = 0;
		pCurrentVesselCell->cConnectIndex[0] =0;
		pCurrentVesselCell->cPericytes =0;
		
		VesselCell* pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
		/*******empty point*********/
		
	SproutNumber = 0; 
	CellListLength = 0;


    int unitsize = 33;
	
	int div = (Xnodes - 1)/2;
	int len = 3;//coverlimit	
	int i=0;
	int j=0;
	int index=0;
			
    int unitindex = 0;//0-3
	int i_art = 0;//1-99
	int j_art = 0;//0,100,200
	int i_ven = 50;//101-199
	int j_ven = 0;//50,150


	/*************************|********************************/																					
																										
																																		
//*********************1*************************/
		j=0;
	  for(i=12;i<187;i=i+25)
	  {
		
	
		len = 0;
		index++;
		CellListLength++;
		pCurrentVesselCell->cCover = 15;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j+len*15;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i;
		pCurrentVesselCell->j[1] = j+1+len*15;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i;
		pCurrentVesselCell->j[2] = j+2+len*15;
		
		pCurrentVesselCell->i[3] = i;
		pCurrentVesselCell->j[3] = j+3+len*15;
		
		pCurrentVesselCell->i[4] = i;
		pCurrentVesselCell->j[4] = j+4+len*15;
		
		pCurrentVesselCell->i[5] = i;
		pCurrentVesselCell->j[5] = j+5+len*15;
		
		pCurrentVesselCell->i[6] = i;
		pCurrentVesselCell->j[6] = j+6+len*15;
		
		pCurrentVesselCell->i[7] = i;
		pCurrentVesselCell->j[7] = j+7+len*15;
		
		pCurrentVesselCell->i[8] = i;
		pCurrentVesselCell->j[8] = j+8+len*15;
		
		pCurrentVesselCell->i[9] = i;
		pCurrentVesselCell->j[9] = j+9+len*15;
		
		pCurrentVesselCell->i[10] = i;
		pCurrentVesselCell->j[10] = j+10+len*15;
        pCurrentVesselCell->i[11] = i;
		pCurrentVesselCell->j[11] = j+11+len*15;
		pCurrentVesselCell->i[12] = i;
		pCurrentVesselCell->j[12] = j+12+len*15;
		pCurrentVesselCell->i[13] = i;
		pCurrentVesselCell->j[13] = j+13+len*15;
		pCurrentVesselCell->i[14] = i;
		pCurrentVesselCell->j[14] = j+14+len*15;



		
		
		    pCurrentVesselCell->cConnection = 1;
			pCurrentVesselCell->cSetPressure =0;
			
			pCurrentVesselCell->cConnectIndex[0] = index+1;	
		
		
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
		
		
		
		//for(len=1;len<20;len++)
		for(len=1;len<13;len++)
		{
			
				index++;
			CellListLength++;
		pCurrentVesselCell->cCover = 15;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j+len*15;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i;
		pCurrentVesselCell->j[1] = j+1+len*15;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i;
		pCurrentVesselCell->j[2] = j+2+len*15;
		
		pCurrentVesselCell->i[3] = i;
		pCurrentVesselCell->j[3] = j+3+len*15;
		
		pCurrentVesselCell->i[4] = i;
		pCurrentVesselCell->j[4] = j+4+len*15;
		
		pCurrentVesselCell->i[5] = i;
		pCurrentVesselCell->j[5] = j+5+len*15;
		
		pCurrentVesselCell->i[6] = i;
		pCurrentVesselCell->j[6] = j+6+len*15;
		
		pCurrentVesselCell->i[7] = i;
		pCurrentVesselCell->j[7] = j+7+len*15;
		
		pCurrentVesselCell->i[8] = i;
		pCurrentVesselCell->j[8] = j+8+len*15;
		
		pCurrentVesselCell->i[9] = i;
		pCurrentVesselCell->j[9] = j+9+len*15;			
			
		pCurrentVesselCell->i[10] = i;
		pCurrentVesselCell->j[10] = j+10+len*15;
        pCurrentVesselCell->i[11] = i;
		pCurrentVesselCell->j[11] = j+11+len*15;
		pCurrentVesselCell->i[12] = i;
		pCurrentVesselCell->j[12] = j+12+len*15;
		pCurrentVesselCell->i[13] = i;
		pCurrentVesselCell->j[13] = j+13+len*15;
		pCurrentVesselCell->i[14] = i;
		pCurrentVesselCell->j[14] = j+14+len*15;

				pCurrentVesselCell->cConnection = 2;
				pCurrentVesselCell->cSetPressure =0;
				pCurrentVesselCell->cConnectIndex[0] = index-1;
				pCurrentVesselCell->cConnectIndex[1] = index+1;	
			

			
			pCurrentVesselCell->cPericytes =0;
			
			
			pNewVesselCell = new VesselCell;
			pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
			pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
			pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
			
		}
		
		
	     
        index++;
		CellListLength++;
		pCurrentVesselCell->cCover = 6;
		pCurrentVesselCell->cIndex = index;
		
	
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j+len*15;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i;
		pCurrentVesselCell->j[1] = j+1+len*15;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i;
		pCurrentVesselCell->j[2] = j+2+len*15;
		
		pCurrentVesselCell->i[3] = i;
		pCurrentVesselCell->j[3] = j+3+len*15;
		
		pCurrentVesselCell->i[4] = i;
		pCurrentVesselCell->j[4] = j+4+len*15;
		
		pCurrentVesselCell->i[5] = i;
		pCurrentVesselCell->j[5] = j+5+len*15;

		
		
		
		pCurrentVesselCell->cConnection = 1;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;	
		pCurrentVesselCell->cPericytes =1;
		
     		pNewVesselCell = new VesselCell;
			pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
			pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
			pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;

	
		
		}	
				
					
			len = 0;
		index++;
		CellListLength++;
		pCurrentVesselCell->cCover = 15;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j+len*15;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i;
		pCurrentVesselCell->j[1] = j+1+len*15;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i;
		pCurrentVesselCell->j[2] = j+2+len*15;
		
		pCurrentVesselCell->i[3] = i;
		pCurrentVesselCell->j[3] = j+3+len*15;
		
		pCurrentVesselCell->i[4] = i;
		pCurrentVesselCell->j[4] = j+4+len*15;
		
		pCurrentVesselCell->i[5] = i;
		pCurrentVesselCell->j[5] = j+5+len*15;
		
		pCurrentVesselCell->i[6] = i;
		pCurrentVesselCell->j[6] = j+6+len*15;
		
		pCurrentVesselCell->i[7] = i;
		pCurrentVesselCell->j[7] = j+7+len*15;
		
		pCurrentVesselCell->i[8] = i;
		pCurrentVesselCell->j[8] = j+8+len*15;
		
		pCurrentVesselCell->i[9] = i;
		pCurrentVesselCell->j[9] = j+9+len*15;
        pCurrentVesselCell->i[10] = i;
		pCurrentVesselCell->j[10] = j+10+len*15;
        pCurrentVesselCell->i[11] = i;
		pCurrentVesselCell->j[11] = j+11+len*15;
		pCurrentVesselCell->i[12] = i;
		pCurrentVesselCell->j[12] = j+12+len*15;
		pCurrentVesselCell->i[13] = i;
		pCurrentVesselCell->j[13] = j+13+len*15;
		pCurrentVesselCell->i[14] = i;
		pCurrentVesselCell->j[14] = j+14+len*15;


		
		
		    pCurrentVesselCell->cConnection = 1;
			pCurrentVesselCell->cSetPressure =0;
			
			pCurrentVesselCell->cConnectIndex[0] = index+1;	
		
		
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
		
		
		
		for(len=1;len<13;len++)
		{
			
				index++;
			CellListLength++;
		pCurrentVesselCell->cCover = 15;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j+len*15;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i;
		pCurrentVesselCell->j[1] = j+1+len*15;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i;
		pCurrentVesselCell->j[2] = j+2+len*15;
		
		pCurrentVesselCell->i[3] = i;
		pCurrentVesselCell->j[3] = j+3+len*15;
		
		pCurrentVesselCell->i[4] = i;
		pCurrentVesselCell->j[4] = j+4+len*15;
		
		pCurrentVesselCell->i[5] = i;
		pCurrentVesselCell->j[5] = j+5+len*15;
		
		pCurrentVesselCell->i[6] = i;
		pCurrentVesselCell->j[6] = j+6+len*15;
		
		pCurrentVesselCell->i[7] = i;
		pCurrentVesselCell->j[7] = j+7+len*15;
		
		pCurrentVesselCell->i[8] = i;
		pCurrentVesselCell->j[8] = j+8+len*15;
		
		pCurrentVesselCell->i[9] = i;
		pCurrentVesselCell->j[9] = j+9+len*15;			
			
		pCurrentVesselCell->i[10] = i;
		pCurrentVesselCell->j[10] = j+10+len*15;
        pCurrentVesselCell->i[11] = i;
		pCurrentVesselCell->j[11] = j+11+len*15;
		pCurrentVesselCell->i[12] = i;
		pCurrentVesselCell->j[12] = j+12+len*15;
		pCurrentVesselCell->i[13] = i;
		pCurrentVesselCell->j[13] = j+13+len*15;
		pCurrentVesselCell->i[14] = i;
		pCurrentVesselCell->j[14] = j+14+len*15;
	
				pCurrentVesselCell->cConnection = 2;
				pCurrentVesselCell->cSetPressure =0;
				pCurrentVesselCell->cConnectIndex[0] = index-1;
				pCurrentVesselCell->cConnectIndex[1] = index+1;	
			

			
			pCurrentVesselCell->cPericytes =0;
			
			
			pNewVesselCell = new VesselCell;
			pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
			pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
			pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
			
		}
		
		
	     
        index++;
		CellListLength++;
		pCurrentVesselCell->cCover = 6;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j+len*15;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i;
		pCurrentVesselCell->j[1] = j+1+len*15;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i;
		pCurrentVesselCell->j[2] = j+2+len*15;
		
		pCurrentVesselCell->i[3] = i;
		pCurrentVesselCell->j[3] = j+3+len*15;
		
		pCurrentVesselCell->i[4] = i;
		pCurrentVesselCell->j[4] = j+4+len*15;
		
		pCurrentVesselCell->i[5] = i;
		pCurrentVesselCell->j[5] = j+5+len*15;

		
		
		
		pCurrentVesselCell->cConnection = 1;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;	
		pCurrentVesselCell->cPericytes =1;
								
		
	
	
			
		
	 pCurrentVesselCell->pNextVesselCell = NULL;


	 pLastVesselCell = pCurrentVesselCell;
	 
	
	  
	

		//pLastVesselPoint = pCurrentVesselPoint->pPreviousVesselPoint;	
	//pLastVesselPoint -> pNextVesselPoint = NULL;

    }
	
	else
	{
		//meixianghao
		
	}
	//cout <<"function InitializeVesselListInOrder finished"<<endl; 
	
	
}



/*


bool CellVesselStructure::SaveVesselList()
{
//cout <<"function SaveListInOrder running"<<endl; 

extern bool AngiogenesisIsStarted;
extern bool AngiogenesisDisabled;

if( AngiogenesisIsStarted == false ||
     AngiogenesisDisabled == true )
 { 
 return false; 
 }	
 
 extern char SaveDirectory[1025];
 char temp_string[1024];
 
 sprintf( temp_string, "%s/angio_resume.dat", SaveDirectory);
 ofstream angio_resume( temp_string, ios::out );
 angio_resume << AngioDeltaT <<"\t"
			  
              		  
             << endl;	
cout<<AngioDeltaT<<" save  "<<endl;				  
 angio_resume.close();	
 
 
 //save list now
  sprintf( temp_string , "%s/pVesselList_resume.dat" , SaveDirectory );
 ofstream pVesselList_resume( temp_string, ios::out );

 VesselCell* pCurrentVesselCell = pFirstVesselCell;
  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;

 while(pCurrentVesselCell)
 {
 pVesselList_resume << pCurrentVesselCell->i<<"\t"
				    << pCurrentVesselCell->j<<endl;
					
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 }
 
 pVesselList_resume.close();
// cout <<"function SaveListInOrder finished"<<endl;
 sprintf( temp_string , "%s/pVesselDistance.dat" , SaveDirectory );
 ofstream pVesselDistance( temp_string, ios::out );
 
 for(int i = 1; i< CellListLength; i++)
 {
 for(int j = 1; j< CellListLength; j++)
 {
	 pVesselDistance<< *Distance(i,j)<<"\t";
 }
 pVesselDistance<< endl;

 }
 
  return true;
 }
 

void CellVesselStructure::GenerateMatrix()
{

int i=0;
int j=0;
int N=0;
double d_x=0;
double d_y=0;
/*
Distance = zeros(CellListLength,CellListLength);
for(i=0;i<CellListLength;i++)
{
	for(j=0;j<CellListLength;j++)
	{
	*Distance(i,j) = -1;
	}
}



pRadius = zeros(CellListLength,CellListLength);
pPermeability = zeros(CellListLength,CellListLength);
pConduct = zeros(CellListLength,CellListLength);
pFlow = zeros(CellListLength,CellListLength);
pPericytes = zeros(CellListLength,CellListLength);

 VesselCell* pCurrentVesselCell = pFirstVesselCell;

 while(pCurrentVesselCell)
 {
 i = pCurrentVesselCell-> cIndex;
 N = pCurrentVesselCell->cConnection;
 for(int k = 0; k < N; k++)
 {
 j = pCurrentVesselCell->cConnectIndex[k];
 
 *Distance(i,j)=1;

 }
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 }
*/
 
/*

 BloodVesselIndicator = zeros( Xnodes , Ynodes );
 BloodVesselIndicatorX = zeros( Xnodes , Ynodes );
 BloodVesselIndicatorY = zeros( Xnodes , Ynodes );
 HaematocritLevel = zeros( Xnodes , Ynodes );
 SproutTips = zeros( Xnodes , Ynodes );
 SproutAge = zeros(Xnodes, Ynodes);
 VesselAge = zeros(Xnodes, Ynodes);
 BloodVesselRadiusXdir = zeros(Xnodes , Ynodes);
 BloodVesselRadiusYdir = zeros(Xnodes , Ynodes);
 BloodVesselRadius = zeros( Xnodes , Ynodes );
 BloodNodePressure = zeros( Xnodes, Ynodes );
 BloodVesselPressureX = zeros( Xnodes, Ynodes );
//Direction Matrix only matters to Sprouts
    //DirectionX = zeros( Xnodes, Ynodes ); // 0 means no vessel, 1, -1 means one way direction
	//DirectionY = zeros( Xnodes, Ynodes );
	
	FlowX = zeros( Xnodes, Ynodes );
	FlowY = zeros( Xnodes, Ynodes );
	WssX = zeros( Xnodes, Ynodes );
	WssY = zeros( Xnodes, Ynodes );
	
	VisX = ones( Xnodes, Ynodes );
	VisY = ones( Xnodes, Ynodes );
	ConductX = zeros( Xnodes, Ynodes );
	ConductY = zeros( Xnodes, Ynodes );
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
	
	//Min's Notes 1: Here is with the position of the ARTERY, fix here when changing position


	
	
	

	
	

  
	cout <<"function TransformMatrix finished"<<endl; 
}
*/
void CellVesselStructure::MemoShuffleFate(Matrix& Signal)
{
	int i=0;
	int j=0;
	double Threshold=0.0000001;
	
	//cout<<"TAF"<<*TAF(3,3)<<endl;
	/**All Stable defaultset**/
	VesselCell* pCurrentVesselCell = pFirstVesselCell;
	pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	
 //while(pCurrentVesselCell)
// {
 
	//cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<"   "<<pCurrentVesselCell->i<<"   "<<pCurrentVesselCell->j<<endl;
 //  if(pCurrentVesselCell->cCover == pCurrentVesselCell->coverlimit)
  // {
//	pCurrentVesselCell->cFate = 'S';
 //  }	
  					
// pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 //} 


		//getchar();			
	
	/****S\T****/
 // pCurrentVesselCell = pFirstVesselCell;
 //pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	
	
	 while(pCurrentVesselCell)
 {
 
  if(pCurrentVesselCell->cFate == 'S')
   {
 MemoSetTipFate(pCurrentVesselCell, Signal, 'T', 'S',Threshold);
   }

//   SetTipFate(pCurrentVesselCell, Signal, 'S', 'S',Threshold);
	//	cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<"   "<<pCurrentVesselCell->i<<"   "<<pCurrentVesselCell->j<<endl;			
									
		//cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<endl;				
																														
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 } 
 
// getchar();
 /*****T\P****/
  pCurrentVesselCell = pFirstVesselCell;
  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	
 VesselCell* pCurrentVesselCell2 = pFirstVesselCell;
 pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;	
 int Index = 0;
	
	 while(pCurrentVesselCell)
 {
   if( pCurrentVesselCell->cFate == 'T')
   {
   for(int k=0; k< pCurrentVesselCell-> cConnection; k++)
   {
     Index = pCurrentVesselCell-> cConnectIndex[k];
	 //cout<<pCurrentVesselCell->cIndex<<":"<<Index<<endl;
	 pCurrentVesselCell2 = FindCell(Index);
	 if ( pCurrentVesselCell2->cFate == 'T' )
	 {
		MemoCompeteFate(pCurrentVesselCell, pCurrentVesselCell2, Signal, 'T', 'P', 'P')	;
		//MemoCompeteFate(pCurrentVesselCell, pCurrentVesselCell2, Signal, 'T', 'T', 'T')	;	
	 }
   }
   
   }
						
						
	//	cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<"   "<<pCurrentVesselCell->i<<"   "<<pCurrentVesselCell->j<<endl;
		//cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<endl;																													
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 } 
// getchar();	
 /************P\T***********/
 
   pCurrentVesselCell = pFirstVesselCell;
  pCurrentVesselCell2 = pFirstVesselCell;
  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	
  pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;	
    bool All_Not_T = 1; 

	 while(pCurrentVesselCell)
 {
  
   if( pCurrentVesselCell->cFate == 'P')
   {
   	
   
    All_Not_T = 1; 
   for(int k=0; k< pCurrentVesselCell-> cConnection; k++)
   {
     Index = pCurrentVesselCell-> cConnectIndex[k];
	 pCurrentVesselCell2 = FindCell(Index);
	
	
	 //Look at this
	 if (pCurrentVesselCell2->cFate == 'T' )
	 {
	
		All_Not_T = 0;	
	 }
   }
   
   if(All_Not_T==1)
   {
   pCurrentVesselCell->cFate = 'T';
   
   }
   
   }
						
		//cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<endl;
	
					
																															
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 } 

cout<<"after shuffle"<<endl;
 
 
 
}

void CellVesselStructure::ShuffleFate(Matrix& Signal)
{
	int i=0;
	int j=0;
	double Threshold=0.0000001;
	
	//cout<<"TAF"<<*TAF(3,3)<<endl;
	/**All Stable defaultset**/
	VesselCell* pCurrentVesselCell = pFirstVesselCell;
	pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	
 while(pCurrentVesselCell)
 {
 
	//cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<"   "<<pCurrentVesselCell->i<<"   "<<pCurrentVesselCell->j<<endl;
 
	pCurrentVesselCell->cFate = 'S';
	
  					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 } 


		//getchar();			
	
	/****S\T****/
  pCurrentVesselCell = pFirstVesselCell;
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	
	
	 while(pCurrentVesselCell)
 {
 
 SetTipFate(pCurrentVesselCell, Signal, 'T', 'S',Threshold);

//   SetTipFate(pCurrentVesselCell, Signal, 'S', 'S',Threshold);
	//	cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<"   "<<pCurrentVesselCell->i<<"   "<<pCurrentVesselCell->j<<endl;			
									
		//cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<endl;				
																														
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 } 
 
// getchar();
 /*****T\P****/
  pCurrentVesselCell = pFirstVesselCell;
  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	
 VesselCell* pCurrentVesselCell2 = pFirstVesselCell;
 pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;	
 int Index = 0;
	
	 while(pCurrentVesselCell)
 {
   if( pCurrentVesselCell->cFate == 'T')
   {
   for(int k=0; k< pCurrentVesselCell-> cConnection; k++)
   {
     Index = pCurrentVesselCell-> cConnectIndex[k];
	 //cout<<pCurrentVesselCell->cIndex<<":"<<Index<<endl;
	 pCurrentVesselCell2 = FindCell(Index);
	 if ( pCurrentVesselCell2->cFate == 'T' )
	 {
		CompeteFate(pCurrentVesselCell, pCurrentVesselCell2, Signal, 'T', 'P', 'P')	;	
	 }
   }
   
   }
						
						
	//	cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<"   "<<pCurrentVesselCell->i<<"   "<<pCurrentVesselCell->j<<endl;
		//cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<endl;																													
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 } 
// getchar();	
 /************P\T***********/
 
   pCurrentVesselCell = pFirstVesselCell;
  pCurrentVesselCell2 = pFirstVesselCell;
  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	
  pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;	
    bool All_Not_T = 1; 

	 while(pCurrentVesselCell)
 {
  
   if( pCurrentVesselCell->cFate == 'P')
   {
   	
   
    All_Not_T = 1; 
   for(int k=0; k< pCurrentVesselCell-> cConnection; k++)
   {
     Index = pCurrentVesselCell-> cConnectIndex[k];
	 pCurrentVesselCell2 = FindCell(Index);
	
	
	 //Look at this
	 if (pCurrentVesselCell2->cFate == 'T' )
	 {
	
		All_Not_T = 0;	
	 }
   }
   
   if(All_Not_T==1)
   {
   pCurrentVesselCell->cFate = 'T';
   
   }
   
   }
						
		//cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<endl;
	
					
																															
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 } 

cout<<"after shuffle"<<endl;
 
 
 
}

void CellVesselStructure::MemoSetSprout(Matrix& Signal)
{
int i1=0;
int j1=0;
int i_max=0;
int j_max=0;
double  Max_a = 0;
double Max_Temp = 0;
extern int Xnodes, Ynodes;
extern Matrix SproutTips;
//SproutTips = zeros(Xnodes,Ynodes);

VesselCell*  pCurrentVesselCell = pFirstVesselCell;
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	
	
	 while(pCurrentVesselCell)
 {
 Max_a = 0;
 
 //if((pCurrentVesselCell->cFate == 'T')&&(pCurrentVesselCell->cCover==pCurrentVesselCell->coverlimit) )
 if((pCurrentVesselCell->cFate == 'T') )
 {
for(int ii=0; ii < pCurrentVesselCell->cCover; ii++)
{
i1 = pCurrentVesselCell->i[ii];
j1 = pCurrentVesselCell->j[ii];
Max_Temp = 0;
//if(*Signal(i1,j1)>Max_a)
//{
//Max_a = *Signal(i1,j1);
//i_max=i1;
//j_max=j1;
//cout<<"Sig  "<<*Signal(i1,j1)<<Max_a<<" max i "<<i_max<<" max j  "<<j_max<<endl;
//}
if(i1<(Xnodes-1))
{
if(*Signal(i1+1,j1)-*Signal(i1,j1)>0)
{
Max_Temp += *Signal(i1+1,j1)-*Signal(i1,j1);

//cout<<"Sig  "<<*Signal(i1,j1)<<Max_a<<" max i "<<i_max<<" max j  "<<j_max<<endl;
}
}

if(i1>0)
{
if(*Signal(i1-1,j1)-*Signal(i1,j1)>0)
{
Max_Temp += *Signal(i1-1,j1)-*Signal(i1,j1);

//cout<<"Sig  "<<*Signal(i1,j1)<<Max_a<<" max i "<<i_max<<" max j  "<<j_max<<endl;
}
}
if(j1<(Ynodes-1))
{
if(*Signal(i1,j1+1)-*Signal(i1,j1)>0)
{
Max_Temp += *Signal(i1,j1+1)-*Signal(i1,j1);

//cout<<"Sig  "<<*Signal(i1,j1)<<Max_a<<" max i "<<i_max<<" max j  "<<j_max<<endl;
}
}
if(j1>0)
{
if(*Signal(i1,j1-1)-*Signal(i1,j1)>0)
{
Max_Temp += *Signal(i1,j1-1)-*Signal(i1,j1);

//cout<<"Sig  "<<*Signal(i1,j1)<<Max_a<<" max i "<<i_max<<" max j  "<<j_max<<endl;
}
}
if(Max_Temp>Max_a)
{
i_max=i1;
j_max=j1;
 Max_a = Max_Temp;
}

}
/*
for(int ii=0; ii < pCurrentVesselCell->cCover; ii++)
{
i1 = pCurrentVesselCell->i[ii];
j1 = pCurrentVesselCell->j[ii];
if(*SproutTips(i1,j1)>0)
{
i_max=i1;
j_max=j1;
//cout<<"Sig  "<<*Signal(i1,j1)<<Max_a<<" max i "<<i_max<<" max j  "<<j_max<<endl;
}

}
*/
if(Max_a>0)
{
*SproutTips(i_max,j_max) = 1;
}

}									
																															
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 } 





}


void CellVesselStructure::SetSprout(Matrix& Signal)
{
int i1=0;
int j1=0;
int i_max=0;
int j_max=0;
double  Max_a = 0;
extern int Xnodes, Ynodes;
extern Matrix SproutTips;
SproutTips = zeros(Xnodes,Ynodes);

VesselCell*  pCurrentVesselCell = pFirstVesselCell;
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	
	
	 while(pCurrentVesselCell)
 {
 Max_a = 0;
 
 if(pCurrentVesselCell->cFate == 'T')
 {
for(int ii=0; ii < pCurrentVesselCell->cCover; ii++)
{
i1 = pCurrentVesselCell->i[ii];
j1 = pCurrentVesselCell->j[ii];
if(*Signal(i1,j1)>Max_a)
{
Max_a = *Signal(i1,j1);
i_max=i1;
j_max=j1;
//cout<<"Sig  "<<*Signal(i1,j1)<<Max_a<<" max i "<<i_max<<" max j  "<<j_max<<endl;
}

}


if(Max_a>0)
{
*SproutTips(i_max,j_max) = 1;
}

}									
																															
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 } 





}

/*
void CellVesselStructure::GrowSprout(Matrix& Signal)
{

int I=0;
int J=0;
int i=0;
int j=0;
int k=0;
int original_length = CellListLength;
double v_l=0;
double v_r=0;
double v_u=0;
double v_d=0;
double vx=0;
double vy=0;
double v=0;
double mu = 1000;
extern int Xnodes, Ynodes;
bool grow = 0;
bool tgrow = 0;
    
	VesselCell* pCurrentVesselCell =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell1 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell2 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell3 =  pLastVesselCell;
	
	VesselCell* pNewVesselCell;
	VesselCell* pTempVesselCell;
	
	

 for(int kk=0; kk<original_length; kk++)
 {
	i = pCurrentVesselCell->i;
    j = pCurrentVesselCell->j;
 
    
	if(pCurrentVesselCell->cFate == 'T'&& (i>1)&&(i<Xnodes-2)&&(j>1)&&(j<Ynodes-2))
   {
	//cout<<"enter sprouttip loop"<<endl;
	    		
		tgrow = 0;
		
		
	v_l = mu*(*Signal(i-1,j)-*Signal(i,j));
	v_r = mu*(*Signal(i+1,j)-*Signal(i,j));
	v_d = mu*(*Signal(i,j-1)-*Signal(i,j));
	v_u = mu*(*Signal(i,j+1)-*Signal(i,j));
		
	
	if(v_l > 0 && v_r > 0)
	{
	
	if(v_l > v_r)
	{
	vx = -v_l;
	}
	else
	{
	vx = v_r;
	}
	
	}
	else if(v_l > 0 && v_r <= 0)
	{
	vx = -v_l;
	}
	else if(v_l <= 0 && v_r > 0)
	{
	vx = v_r;
	}
	else
	{
	vx = 0;
	}
	
	
	if(v_d > 0 && v_u > 0)
	{
	
	if(v_d > v_u)
	{
	vy = -v_d;
	}
	else
	{
	vy = v_u;
	}
	
	}
	else if(v_d > 0 && v_u <= 0)
	{
	vy = -v_d;
	}
	else if(v_d <= 0 && v_u > 0)
	{
	vy = v_u;
	}
	else
	{
	vy = 0;
	}
	
//do two times of three situation (1) taken and connected (2) taken and unconnected (3)not taken

//

   if(min_abs(vx) > 0 && min_abs(vy) > 0)
	{
	
	if(min_abs(vx) > min_abs(vy))
	{
	if(vx>0)
	{
	I = i+1;
	J = j;
	}
	else
	{
	I = i-1;
	J = j;
	}
	
	if(*Indicator(I,J)==1)//taken
	{
	pCurrentVesselCell1 = FindCell(I,J);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	else//
	{
	if(vy>0)
	{
	I = i;
	J = j+1;
	}
	else
	{
	I = i;
	J = j-1;
	}
	
	if(*Indicator(I,J)==1)//taken
	{
	pCurrentVesselCell1 = FindCell(I,J);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	}
	else//vacant
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	 pCurrentVesselCell2-> cFate = 'S';
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = I ;
	  pCurrentVesselCell3->j = J ;
      pCurrentVesselCell3->cIndex = CellListLength;
	   CellListLength++;
	  *Indicator(I,J) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	Connect(pCurrentVesselCell , pCurrentVesselCell3);
	grow=1;
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
	
	}
	
	}
	}
	else//vacant
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	 pCurrentVesselCell2-> cFate = 'S';
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = I ;
	  pCurrentVesselCell3->j = J ;
      pCurrentVesselCell3->cIndex = CellListLength;
	   CellListLength++;
	  *Indicator(I,J) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	Connect(pCurrentVesselCell , pCurrentVesselCell3);
	grow=1;
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
	}
	
	
	}//vx>vy
	else//vy>vx
	{
	if(vy>0)
	{
	I = i;
	J = j+1;
	}
	else
	{
	I = i;
	J = j-1;
	}
	
	if(*Indicator(I,J)==1)//taken
	{
	pCurrentVesselCell1 = FindCell(I,J);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	else
	{
	if(vx>0)
	{
	I = i+1;
	J = j;
	}
	else
	{
	I = i-1;
	J = j;
	}
	
	if(*Indicator(I,J)==1)//taken
	{
	pCurrentVesselCell1 = FindCell(I,J);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	}
	else//vacant
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	 pCurrentVesselCell2-> cFate = 'S';
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = I ;
	  pCurrentVesselCell3->j = J ;
      pCurrentVesselCell3->cIndex = CellListLength;
	   CellListLength++;
	  *Indicator(I,J) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	Connect(pCurrentVesselCell , pCurrentVesselCell3);
	grow=1;
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
	
	}

	
	}
	}
	else//vacant
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	 pCurrentVesselCell2-> cFate = 'S';
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = I ;
	  pCurrentVesselCell3->j = J ;
      pCurrentVesselCell3->cIndex = CellListLength;
	   CellListLength++;
	  *Indicator(I,J) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	Connect(pCurrentVesselCell , pCurrentVesselCell3);
	grow=1;
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
	
	}

	}
	
	}//two directions both probable
	
	else if(min_abs(vx) > 0 && min_abs(vy) <= 0)
	{
	if(vx>0)
	{
	I = i+1;
	J = j;
	}
	else
	{
	I = i-1;
	J = j;
	}
	
	
	if(*Indicator(I,J)==1)//taken
	{
	pCurrentVesselCell1 = FindCell(I,J);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	
	}
	else//vacant
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	 pCurrentVesselCell2-> cFate = 'S';
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = I ;
	  pCurrentVesselCell3->j = J ;
      pCurrentVesselCell3->cIndex = CellListLength;
	   CellListLength++;
	  *Indicator(I,J) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	Connect(pCurrentVesselCell , pCurrentVesselCell3);
	grow=1;
	 }//if there is proliferation
	 k++;
	
	//while sweep neibour
	
	}

	
	}

	
	}//only one direction
	else if(min_abs(vx)<=0 && min_abs(vy)> 0)
	{
	
	if(vy>0)
	{
	I = i;
	J = j+1;
	}
	else
	{
	I = i;
	J = j-1;
	}
	
	if(*Indicator(I,J)==1)//taken
	{
	pCurrentVesselCell1 = FindCell(I,J);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	}
	else//vacant
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	 pCurrentVesselCell2-> cFate = 'S';
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = I ;
	  pCurrentVesselCell3->j = J ;
      pCurrentVesselCell3->cIndex = CellListLength;
	   CellListLength++;
	  *Indicator(I,J) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	Connect(pCurrentVesselCell , pCurrentVesselCell3);
	grow=1;
	 }//if there is proliferation
	 k++;
	
	//while sweep neibour
	
	}

	
	}

	
	}//only one direction
	
	
	//
	}//if tip cell
					


					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 }//while sweep

	
	pLastVesselCell = pCurrentVesselCell3;
			


 }
 


void CellVesselStructure::Grow_One(Matrix& Signal)//take place step by step
{

int i=0;
int j=0;
int k=0;
int original_length = CellListLength;
double v_l=0;
double v_r=0;
double v_u=0;
double v_d=0;
double vx=0;
double vy=0;
double v=0;
double mu = 1000;
extern int Xnodes, Ynodes;
bool grow = 0;
    
	VesselCell* pCurrentVesselCell =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell1 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell2 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell3 =  pLastVesselCell;
	
	VesselCell* pNewVesselCell;

 for(int kk=0; kk<original_length; kk++)
 {
 



   if(pCurrentVesselCell->cFate == 'T')
   {
   	 
   	i = pCurrentVesselCell->i;
    j = pCurrentVesselCell->j;
	v_l = mu*(*Signal(i-1,j)-*Signal(i,j));
	v_r = mu*(*Signal(i+1,j)-*Signal(i,j));
	v_d = mu*(*Signal(i,j-1)-*Signal(i,j));
	v_u = mu*(*Signal(i,j+1)-*Signal(i,j));
		
	
	if(v_l > 0 && v_r > 0)
	{
	
	if(v_l > v_r)
	{
	vx = -min_Round(v_l);
	}
	else
	{
	vx = min_Round(v_r);
	}
	
	}
	else if(v_l > 0 && v_r <= 0)
	{
	vx = -min_Round(v_l);
	}
	else if(v_l <= 0 && v_r > 0)
	{
	vx = min_Round(v_r);
	}
	else
	{
	vx = 0;
	}
	
	
	if(v_d > 0 && v_u > 0)
	{
	
	if(v_d > v_u)
	{
	vy = -min_Round(v_d);
	}
	else
	{
	vy = min_Round(v_u);
	}
	
	}
	else if(v_d > 0 && v_u <= 0)
	{
	vy = -min_Round(v_d);
	}
	else if(v_d <= 0 && v_u > 0)
	{
	vy = min_Round(v_u);
	}
	else
	{
	vy = 0;
	}

	
	
	//cout<<"vx: "<<vx<<endl;
	//cout<<"vy: "<<vy<<endl;
	
	
	if(*Indicator(i,j)==1)
	{
	pCurrentVesselCell1 = FindCell(i,j);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	}
	else if(*Indicator(i,j)==0)
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	
	//cout<<"k"<<k<<"   cIndex"<<pCurrentVesselCell->cIndex;
	//getchar();
	
	//cout<<"point to"<<pCurrentVesselCell->cConnectIndex[k];
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	//cout<<"point to"<<pCurrentVesselCell2->cIndex;
	//getchar();
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	  	  
	

	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = i ;
	  pCurrentVesselCell3->j = j ;
      pCurrentVesselCell3->cIndex = CellListLength ;
	   CellListLength++;
	  *Indicator(i,j) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	 //pCurrentVesselCell3->cConnection = 1;

	 // pCurrentVesselCell3->cConnectIndex[0] = pCurrentVesselCell->cIndex;
	 Connect(pCurrentVesselCell , pCurrentVesselCell3);
	 
	 
   
	
	
	grow=1;
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
	
	
	//v=pow((pow(vx,2)+pow(vy,2)),0.5);
	 

	}//if vacant
	
   }//if tip cell
					



					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 }//while sweep

	
	pLastVesselCell = pCurrentVesselCell3;
			
	
 }
void CellVesselStructure::Grow_zero(Matrix& Signal)//take place only
{

int i=0;
int j=0;
int k=0;
int original_length = CellListLength;
double vx=0;
double vy=0;
double v=0;
double mu = 1000;
extern int Xnodes, Ynodes;
bool grow = 0;
    
	VesselCell* pCurrentVesselCell =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell1 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell2 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell3 =  pLastVesselCell;
	
	VesselCell* pNewVesselCell;

 for(int kk=0; kk<original_length; kk++)
 {
 



   if(pCurrentVesselCell->cFate == 'T')
   {
   	 
   	i = pCurrentVesselCell->i;
    j = pCurrentVesselCell->j;
	vx = min_Round(mu*(*Signal(i+1,j)-*Signal(i-1,j)));
	vy = min_Round(mu*(*Signal(i,j+1)-*Signal(i,j-1)));
	//cout<<"vx: "<<vx<<endl;
	//cout<<"vy: "<<vy<<endl;
	i=i+vx;
	j=j+vy;
	
	if(*Indicator(i,j)==1)
	{
	pCurrentVesselCell1 = FindCell(i,j);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	}
	else if(*Indicator(i,j)==0)
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	
	//cout<<"k"<<k<<"   cIndex"<<pCurrentVesselCell->cIndex;
	//getchar();
	
	//cout<<"point to"<<pCurrentVesselCell->cConnectIndex[k];
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	//cout<<"point to"<<pCurrentVesselCell2->cIndex;
	//getchar();
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	  	  
	

	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = i ;
	  pCurrentVesselCell3->j = j ;
      pCurrentVesselCell3->cIndex = CellListLength ;
	   CellListLength++;
	  *Indicator(i,j) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	 //pCurrentVesselCell3->cConnection = 1;

	 // pCurrentVesselCell3->cConnectIndex[0] = pCurrentVesselCell->cIndex;
	 Connect(pCurrentVesselCell , pCurrentVesselCell3);
	 
	 
   
	
	
	grow=1;
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
	
	
	//v=pow((pow(vx,2)+pow(vy,2)),0.5);
	 

	}//if vacant
	
   }//if tip cell
					



					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 }//while sweep

	
	pLastVesselCell = pCurrentVesselCell3;
			
	
 }
void CellVesselStructure::Grow_Deprecate(Matrix& Signal)//take place only
{

int i=0;
int j=0;
int k=0;
int original_length = CellListLength;
double vx=0;
double vy=0;
double v=0;
double mu = 1000;
extern int Xnodes, Ynodes;
bool grow = 0;
    
	VesselCell* pCurrentVesselCell =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell1 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell2 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell3 =  pLastVesselCell;
	
	VesselCell* pNewVesselCell;

 for(int kk=0; kk<original_length; kk++)
 {
 



   if(pCurrentVesselCell->cFate == 'T')
   {
   	 
   	i = pCurrentVesselCell->i;
    j = pCurrentVesselCell->j;
	vx = min_Round(mu*(*Signal(i+1,j)-*Signal(i-1,j)));
	vy = min_Round(mu*(*Signal(i,j+1)-*Signal(i,j-1)));
	//cout<<"vx: "<<vx<<endl;
	//cout<<"vy: "<<vy<<endl;
	i=i+vx;
	j=j+vy;
	
	if(*Indicator(i,j)==1)
	{
	pCurrentVesselCell1 = FindCell(i,j);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	}
	else if(*Indicator(i,j)==0)
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	
	//cout<<"k"<<k<<"   cIndex"<<pCurrentVesselCell->cIndex;
	//getchar();
	
	//cout<<"point to"<<pCurrentVesselCell->cConnectIndex[k];
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	//cout<<"point to"<<pCurrentVesselCell2->cIndex;
	//getchar();
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	  	  
	

	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = i ;
	  pCurrentVesselCell3->j = j ;
      pCurrentVesselCell3->cIndex = CellListLength ;
	   CellListLength++;
	  *Indicator(i,j) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	 //pCurrentVesselCell3->cConnection = 1;

	 // pCurrentVesselCell3->cConnectIndex[0] = pCurrentVesselCell->cIndex;
	 Connect(pCurrentVesselCell , pCurrentVesselCell3);
	 
	 
   
	
	
	grow=1;
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
	
	
	//v=pow((pow(vx,2)+pow(vy,2)),0.5);
	 

	}//if vacant
	
   }//if tip cell
					



					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 }//while sweep

	
	pLastVesselCell = pCurrentVesselCell3;
			
	
 }
*/ 

/*
 void CellVesselStructure::String_Interact(Matrix& Signal)
{
	int i=0;
	int j=0;
	
	bool all_trapped = 0;
	double tol = 2;
	double error = 10;
	double vx=0;
    double vy=0;
    double v=0;
    double mu = 1000;
	double sticky = 1;
	int Index = 0;
	double deltax = 0;
	double deltay = 0;
	
	VesselCell* pCurrentVesselCell = pFirstVesselCell;
	VesselCell* pCurrentVesselCell1 = pFirstVesselCell;
	VesselCell* pCurrentVesselCell2 = pFirstVesselCell;
	
	while(pCurrentVesselCell)
	{
	if(pCurrentVesselCell->cFate == 'T')
	{
	
	
	i = pCurrentVesselCell->i;
    j = pCurrentVesselCell->j;
	vx = min_Round(mu*(*Signal(i+1,j)-*Signal(i-1,j)));
	vy = min_Round(mu*(*Signal(i,j+1)-*Signal(i,j-1)));
	//cout<<"vx: "<<vx<<endl;
	//cout<<"vy: "<<vy<<endl;
	i=i+vx;
	j=j+vy;
	
	if(*Indicator(i,j)==1)
	{
	pCurrentVesselCell1 = FindCell(i,j);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	}//anastmosis
	else
	{
	pCurrentVesselCell->i=i;
	pCurrentVesselCell->j=j;
	
	}
		
	
	}
	
	
	
	
	pCurrentVesselCell = pCurrentVesselCell-> pNextVesselCell;
	}

	
	int round =0;
	while(!all_trapped&& round<cConnectRange-1 )
	{
    round++;
	all_trapped = 1;
	cout<<"error:  "<<error<<endl;
	getchar();
	//error = 0;
	VesselCell* pCurrentVesselCell = pFirstVesselCell;
	VesselCell* pCurrentVesselCell1 = pFirstVesselCell;
	VesselCell* pCurrentVesselCell2 = pFirstVesselCell;
	
       while(pCurrentVesselCell)
       {
 
	cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<"   "<<pCurrentVesselCell->i<<"   "<<pCurrentVesselCell->j<<endl;
	getchar();
			if(pCurrentVesselCell->cPericytes==0&&pCurrentVesselCell->cFate != 'D'&&pCurrentVesselCell->cFate != 'T')
			{
		
		
			vx = 0;
			vy = 0;
			
			 for(int k=0; k< pCurrentVesselCell-> cConnection; k++)
             {
					Index = pCurrentVesselCell-> cConnectIndex[k];
					pCurrentVesselCell2 = FindCell(Index);
					deltax = sticky*(pCurrentVesselCell2->i - pCurrentVesselCell->i );
					deltay = sticky*(pCurrentVesselCell2->j - pCurrentVesselCell->j );
					vx = vx + deltax;
					cout<<"vx"<<vx<<endl;

					vy = vy + deltay;
			
			       cout<<"vy"<<vy<<endl;
			}
			
			/*
			vx=(vx);
							if(min_abs(vx)>error)
			{
			error =min_abs(vx);
			}
			vy=(vy);
				
			if(min_abs(vy)>error)
			{
			error = min_abs(vy);
			}
			
			

			// pCurrentVesselCell->i = pCurrentVesselCell->i + vx;
			// pCurrentVesselCell->j = pCurrentVesselCell->j + vy;
			
			if(vx>0.5)
			{
			
			i = pCurrentVesselCell->i + 1;

			}
			else if (vx<-0.5)
			{
			i = pCurrentVesselCell->i - 1;
			}
			
			if(vy>0.5)
			{
			j = pCurrentVesselCell->j + 1;

			}
			else if (vy<-0.5)
			{
			j = pCurrentVesselCell->j - 1;
			}
			
			if(i == pCurrentVesselCell->i&&j == pCurrentVesselCell->j)
			{
			 *Indicator(pCurrentVesselCell->i,pCurrentVesselCell->j)=1;
			}
			else
			{
		
			 if(*Indicator(i,j)==1)
	          {
			  
	          pCurrentVesselCell1 = FindCell(i,j);
	           if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	           {
	           Connect(pCurrentVesselCell,pCurrentVesselCell1);
	           }
			   
			  }
			  else
			  {
			  
			  all_trapped=0;
			  *Indicator(pCurrentVesselCell->i,pCurrentVesselCell->j)=0;
			  pCurrentVesselCell->i=i;
			  pCurrentVesselCell->j=j;
			  *Indicator(pCurrentVesselCell->i,pCurrentVesselCell->j)=1;

			  }
			  
			//  cout<<"This P or S move to"<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<"   "<<pCurrentVesselCell->i<<"   "<<pCurrentVesselCell->j<<endl;
	          //getchar();
          }

		
        	
			}//no pericytes and alive
  			
					
									
					
         pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
	   } 
 
 
    }

}
**/

/*
 void CellVesselStructure::Grow(Matrix& Signal)//in between only
{

int i=0;
int j=0;
int k=0;
int original_length = CellListLength;

double v=0;
double mu = 1000;
extern int Xnodes, Ynodes;
bool grow = 0;
    
	VesselCell* pCurrentVesselCell = pFirstVesselCell;
	VesselCell* pCurrentVesselCell1 =pFirstVesselCell;
	VesselCell* pCurrentVesselCell2 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell3 = pLastVesselCell;
	
	VesselCell* pNewVesselCell;

 for(int kk=0; kk<original_length; kk++)
 {
 
   if(pCurrentVesselCell->cFate == 'T')
   {
   	 
   	i = pCurrentVesselCell->i;
    j = pCurrentVesselCell->j;
	k=0;
	while(k< pCurrentVesselCell->cConnection)
	{   
		  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	cout<<"point to"<<pCurrentVesselCell2->cIndex;
	getchar();
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	 
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = pCurrentVesselCell2->i ;
	  pCurrentVesselCell3->j = pCurrentVesselCell2->j ;
      pCurrentVesselCell3->cIndex = CellListLength ;
	   CellListLength++;
	
	  	  pCurrentVesselCell3->cSetPressure = 0;
	 //pCurrentVesselCell3->cConnection = 1;

	 // pCurrentVesselCell3->cConnectIndex[0] = pCurrentVesselCell->cIndex;
	 cout<<"before"<<endl;
	 DisConnect(pCurrentVesselCell,pCurrentVesselCell2);
	  cout<<"after"<<endl;
	 Connect(pCurrentVesselCell , pCurrentVesselCell3);
	 Connect(pCurrentVesselCell2 , pCurrentVesselCell3);
    pCurrentVesselCell2-> cFate = 'S';
	pCurrentVesselCell3-> cFate = 'S';
	cout<<"Become"<<pCurrentVesselCell2->cIndex;
	cout<<"Become"<<pCurrentVesselCell3->cIndex;
	getchar();
	
	
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
		
   }//if tip cell
	
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 }//for sweep

	
	pLastVesselCell = pCurrentVesselCell3;
			
	
 }

 
 **/
 
 CellVesselStructure* CellVesselStructure::GetAddress(void)
 {
 return this;
 }
 
 VesselCell* CellVesselStructure::FindCell(int Index)
{
VesselCell* pCurrentVesselCell = pFirstVesselCell;
pCurrentVesselCell = pCurrentVesselCell-> pNextVesselCell;
while(pCurrentVesselCell && pCurrentVesselCell->cIndex != Index)
{
pCurrentVesselCell = pCurrentVesselCell-> pNextVesselCell;
}

if(pCurrentVesselCell)
{
return pCurrentVesselCell;
}
else
{
return pFirstVesselCell;
}

}

 VesselCell* CellVesselStructure::FindCell(int i, int j)
{

bool Found = 0;

VesselCell* pCurrentVesselCell = pFirstVesselCell;
//cout<<pCurrentVesselCell->cIndex<<endl;

while(pCurrentVesselCell && !Found)
{
//cout<<pCurrentVesselCell->cIndex<<endl;
pCurrentVesselCell = pCurrentVesselCell-> pNextVesselCell;
for(int ii=0; ii<pCurrentVesselCell->cCover; ii++)
{
if(pCurrentVesselCell->i[ii]==i && pCurrentVesselCell->j[ii]==j)
{
	Found = 1;
}
//cout<<"here2"<<endl;
}




//cout<<"here1"<<endl;
}

 

if(Found == 1)
{
return pCurrentVesselCell;
}
else
{
return pFirstVesselCell;
}


}




 void CellVesselStructure::ColorCell(void)
{
extern Matrix CellColor;
int i = 0;
int j = 0;
extern int Xnodes;
extern int Ynodes;
CellColor = zeros(Xnodes, Ynodes);

VesselCell* pCurrentVesselCell = pFirstVesselCell;
pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
while(pCurrentVesselCell)
{
if(pCurrentVesselCell->cFate == 'T')
{
   for(int ii=0; ii<pCurrentVesselCell->cCover; ii++)
   {
	i = pCurrentVesselCell->i[ii];
	j = pCurrentVesselCell->j[ii];
	
	*CellColor(i,j) = 80;
   }
}
else if(pCurrentVesselCell->cFate == 'S')
{
for(int ii=0; ii<pCurrentVesselCell->cCover; ii++)
   {
	i = pCurrentVesselCell->i[ii];
	j = pCurrentVesselCell->j[ii];
	
	*CellColor(i,j) = 10;//stable
	
   }

}
else if(pCurrentVesselCell->cFate == 'P')
{

for(int ii=0; ii<pCurrentVesselCell->cCover; ii++)
   {
	i = pCurrentVesselCell->i[ii];
	j = pCurrentVesselCell->j[ii];
	
	*CellColor(i,j) = 50;//Proliferation
	
   }
}





pCurrentVesselCell = pCurrentVesselCell-> pNextVesselCell;
}

}

bool Connected(VesselCell* a, VesselCell* b) 
{
for(int j=0;j<(a->cConnection);j++)
{
if(a->cConnectIndex[j]==b->cIndex)
{
 return 1;
}
}

return 0;

}



void Connect(VesselCell* a, VesselCell* b)
{
int i = a->cConnection;

if(i<a->cConnectRange)
{
a->cConnectIndex[i] = b->cIndex;
}

a->cConnection = a->cConnection + 1;
//cout<<a->cConnection<<endl;
i=b->cConnection;
if(i<a->cConnectRange)
{
b->cConnectIndex[i] = a->cIndex;
}
b->cConnection = b->cConnection + 1;
//cout<<b->cConnection<<endl;
}


void DisConnect(VesselCell* a, VesselCell* b)
{
int i = b->cIndex;
int j=0;
while(a->cConnectIndex[j]!=i&&j<a->cConnectRange-1)
{
j++;
}

if(j==(a->cConnectRange-1))
{
  a->cConnectIndex[j]=0;
}
else
{
  while(j<a->cConnectRange-1)
   {
   a->cConnectIndex[j]=a->cConnectIndex[j+1];
   j++;
   }
  a->cConnectIndex[j]=0;
}

a->cConnection = a->cConnection-1;

 i = a->cIndex;
 j=0;
while(b->cConnectIndex[j]!=i&&j<b->cConnectRange-1)
{
j++;
}

if(j==b->cConnectRange-1)
{
  b->cConnectIndex[j]=0;
}
else
{
  while(j<b->cConnectRange-1)
   {
   b->cConnectIndex[j]=b->cConnectIndex[j+1];
   j++;
   }
  b->cConnectIndex[j]=0;
}

b->cConnection = b->cConnection-1;



}

void RemoveUnConnectedPoint(VesselCell* a, int i, int j)
{
  
  int index = 0;
  if(a->cCover > 1)
  {
  while(a->i[index]!=i||a->j[index]!=j)
		{
		index++;
		}
	if(index == (a->cCover - 1))
	{
		a->cCover = a->cCover - 1;
	}
	else
	{	
		while(index < (a->cCover - 1))
		{
			a->i[index] = a->i[index+1];
			a->j[index] = a->j[index+1];
			index++;
		}
		a->cCover = a->cCover - 1;
	}
	
	   
  }
 	  
  
   
}

void MemoCompeteFate(VesselCell* a, VesselCell* b, Matrix& Signal, char Win, char Lose, char Fair)
{


int i1=0;
int j1=0;
int i2=0;
int j2=0;

double Max_a = 0;
double Max_b = 0;
extern Matrix SproutTips;
/*
cout<<a->cIndex;
cout<<endl;
cout<<b->cIndex;
getchar();
*/
if(a->cFate == Win && b->cFate == Win)
{
for(int ii=0; ii < a->cCover; ii++)
{
i1 = a->i[ii];
j1 = a->j[ii];
if(*SproutTips(i1,j1)>0)
{
Max_a = 1;
}
if(*Signal(i1,j1)>Max_a)
{
Max_a = *Signal(i1,j1);
//cout<< Max_a<<"  ";
}

}

for(int ii=0; ii < b->cCover; ii++)
{
i2 = b->i[ii];
j2 = b->j[ii];
if(*SproutTips(i2,j2)>0)
{
Max_b = 1;
}
if(*Signal(i2,j2)>Max_b)
{
Max_b = *Signal(i2,j2);
//cout<< Max_b<<endl;

}

}


if(Max_a>Max_b)
{
a->cFate = Win;
b->cFate = Lose;

}
else if(Max_a==Max_b)
{
a->cFate = Fair;
b->cFate = Fair;

}
else
{

a->cFate = Lose;
b->cFate = Win;

}


//cout<<a->cFate;
//cout<<endl;
//cout<<b->cFate;
//getchar();


}
//cout<<a->cIndex<<"and"<<b->cIndex<<endl;
//cout<<a->cFate<<"and"<<b->cFate<<endl;


}


void CompeteFate(VesselCell* a, VesselCell* b, Matrix& Signal, char Win, char Lose, char Fair)
{


int i1=0;
int j1=0;
int i2=0;
int j2=0;

double Max_a = 0;
double Max_b = 0;
/*
cout<<a->cIndex;
cout<<endl;
cout<<b->cIndex;
getchar();
*/
if(a->cFate == Win && b->cFate == Win)
{
for(int ii=0; ii < a->cCover; ii++)
{
i1 = a->i[ii];
j1 = a->j[ii];
if(*Signal(i1,j1)>Max_a)
{
Max_a = *Signal(i1,j1);
//cout<< Max_a<<"  ";
}

}

for(int ii=0; ii < b->cCover; ii++)
{
i2 = b->i[ii];
j2 = b->j[ii];
if(*Signal(i2,j2)>Max_b)
{
Max_b = *Signal(i2,j2);
//cout<< Max_b<<endl;

}

}


if(Max_a>Max_b)
{
a->cFate = Win;
b->cFate = Lose;

}
else if(Max_a==Max_b)
{
a->cFate = Fair;
b->cFate = Fair;

}
else
{

a->cFate = Lose;
b->cFate = Win;

}


//cout<<a->cFate;
//cout<<endl;
//cout<<b->cFate;
//getchar();


}
//cout<<a->cIndex<<"and"<<b->cIndex<<endl;
//cout<<a->cFate<<"and"<<b->cFate<<endl;


}

void MemoSetTipFate(VesselCell* a, Matrix& Signal, char Win, char Lose, double Threshold)
{
int i1=0;
int j1=0;
double Max_a = 0;
bool coated = 0;
extern Matrix Stability;
extern Matrix SproutTips;
extern Matrix TAF;
extern Matrix Wss;
extern int Xnodes,Ynodes;
double Stable_up = RetrieveDoubleParameter( ParameterFile , "Stable_up");
double Stable_down = RetrieveDoubleParameter( ParameterFile , "Stable_down");
       
	
   
      
for(int ii=0; ii < a->cCover; ii++)
{
i1 = a->i[ii];
j1 = a->j[ii];
//cout<<*TAF(i1,j1);
double RandNumber = float(rand())/float(RAND_MAX);
if(RandNumber<*Stability(i1,j1))
{
 coated = 1;
}

if(*Signal(i1,j1)>Max_a)
{
Max_a = *Signal(i1,j1);
}

}
/*
if(Max_a>Threshold&&(!coated))
{
a->cFate = Win;
//cout<<Max_a<<"and"<<Threshold;
}
else
{
a->cFate = Lose;
//cout<<Lose;
//getchar();
}
*/

double RandNumber = float(rand())/float(RAND_MAX);

if(RandNumber<Max_a)
{

for(int ii=0; ii < a->cCover; ii++)
{
i1 = a->i[ii];
j1 = a->j[ii];
//cout<<*TAF(i1,j1);
*Stability(i1,j1) = *Stability(i1,j1) - AngioDeltaT * Stable_down;
}
if(!coated)
{
a->cFate = Win;
}
else
{
//a->cFate = Lose;
a->cFate = Win;
}
//cout<<Max_a<<"and"<<Threshold;
}
else
{
a->cFate = Lose;
//cout<<Lose;
//getchar();
for(int ii=0; ii < a->cCover; ii++)
{
i1 = a->i[ii];
j1 = a->j[ii];
//cout<<*TAF(i1,j1);
*Stability(i1,j1) = *Stability(i1,j1) + AngioDeltaT * Stable_up;
}

}


}


void SetTipFate(VesselCell* a, Matrix& Signal, char Win, char Lose, double Threshold)//This is momery less
{
int i1=0;
int j1=0;
double Max_a = 0;
bool coated = 0;
extern Matrix Stability;
extern Matrix TAF;
extern Matrix Wss;
extern int Xnodes,Ynodes;
double Stable_up = RetrieveDoubleParameter( ParameterFile , "Stable_up");
double Stable_down = RetrieveDoubleParameter( ParameterFile , "Stable_down");
       
	
   
      
for(int ii=0; ii < a->cCover; ii++)
{
i1 = a->i[ii];
j1 = a->j[ii];
//cout<<*TAF(i1,j1);
double RandNumber = float(rand())/float(RAND_MAX);
if(RandNumber<*Stability(i1,j1))
{
 coated = 1;
}

if(*Signal(i1,j1)>Max_a)
{
Max_a = *Signal(i1,j1);
}

}
/*
if(Max_a>Threshold&&(!coated))
{
a->cFate = Win;
//cout<<Max_a<<"and"<<Threshold;
}
else
{
a->cFate = Lose;
//cout<<Lose;
//getchar();
}
*/

double RandNumber = float(rand())/float(RAND_MAX);

if(RandNumber<Max_a)
{

for(int ii=0; ii < a->cCover; ii++)
{
i1 = a->i[ii];
j1 = a->j[ii];
//cout<<*TAF(i1,j1);
*Stability(i1,j1) = *Stability(i1,j1) - AngioDeltaT * Stable_down;
}
if(!coated)
{
a->cFate = Win;
}
else
{
//a->cFate = Lose;
a->cFate = Win;
}
//cout<<Max_a<<"and"<<Threshold;
}
else
{
a->cFate = Lose;
//cout<<Lose;
//getchar();
for(int ii=0; ii < a->cCover; ii++)
{
i1 = a->i[ii];
j1 = a->j[ii];
//cout<<*TAF(i1,j1);
*Stability(i1,j1) = *Stability(i1,j1) + AngioDeltaT * Stable_up;
}

}


}


void SetTipFate(VesselCell* a, Matrix& Signal, char Win, char Lose)
{
int i1=0;
int j1=0;
double Max_a = 0;


for(int ii=0; ii < a->cCover; ii++)
{
i1 = a->i[ii];
j1 = a->j[ii];
//cout<<*TAF(i1,j1);
if(*Signal(i1,j1)>Max_a)
{
Max_a = *Signal(i1,j1);
}

}


if(Max_a)
{
a->cFate = Win;
//cout<<Max_a<<"and"<<Threshold;
}
else
{
a->cFate = Lose;
//cout<<Lose;
//getchar();
}




}




/**********************************hualidefeigexian**********************************************hualidefeigexian*********************************************/






class AngioSolverPoint{
public:
 int i;
 int j;
 AngioSolverPoint* pNextSolverPoint;
AngioSolverPoint();
};

AngioSolverPoint::AngioSolverPoint()
{ i=0; j=0; pNextSolverPoint = NULL; }






VesselPoint::VesselPoint()
{ 
	i=0;
	j=0; 
	AmIArtery = 0;
	AmIVenous = 0;
	MyAge = 0;
	i_origin = 0;
	j_origin = 0;
	pNextVesselPoint = NULL; 
	pPreviousVesselPoint = NULL;
}

VesselPoint::~VesselPoint()
{
cout<<"destructor";
}

VesselStructure::VesselStructure()
{
	pFirstVesselPoint = NULL;
	pLastVesselPoint = NULL;
	NodeLength = 0;
	CellNumber = 0;
	SproutNumber = 0;
	VesselListLength = 0;
	//cout <<"function VesselStructure running"<<endl;	
}

VesselStructure::~VesselStructure()
{
//cout <<"function ~VesselStructure running"<<endl;	
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 if( pCurrentVesselPoint)
 {
	while(pCurrentVesselPoint)
	{
		pCurrentVesselPoint = pCurrentVesselPoint ->pNextVesselPoint;
		delete  pCurrentVesselPoint->pPreviousVesselPoint;
	}
	delete  pCurrentVesselPoint;
 }
}




int min_Round( double input )
{
if(input>0)
{

  return ImprovedRound(input); 
 }
 else if(input<0)
 {
  return -1*ImprovedRound(-input); 
 }
 else
 {
 return 0;
 }
}


double min_abs(double x)
{
if(x<0) 
{return -x;}
return x;
}



void VesselStructure::VesselInitialization( bool VesselResume, CellVesselStructure* TheVessel )
{
	
	//cout <<"function ~VesselInitialization running"<<endl;	
	extern Matrix X, Y;
	extern int Xnodes, Ynodes;
	extern Matrix BloodVesselIndicator;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix InterPressure;
	extern Matrix CellColor;
	
	extern Matrix BloodVesselRadius;
	//mextern Matrix BloodVesselFlowRate;
	extern Matrix HaematocritLevel;
	extern Matrix SproutTips;
	extern Matrix SproutAge;
	extern Matrix VesselAge;
	extern Matrix BloodVesselRadiusXdir;
	extern Matrix BloodVesselRadiusYdir;
	extern Matrix BloodNodePressure;
	extern Matrix BloodVesselPressureX;
	extern Matrix BloodVesselPressureY;
	//extern Matrix DirectionX, DirectionY;
	extern Matrix FlowX, FlowY;
	extern Matrix WssX, WssY;
	extern int innumbers, outnumbers;
	extern bool Injectionrandomized;
	Injectionrandomized = bool(RetrieveDoubleParameter( ParameterFile , "Injectionrandomized"));
	
	innumbers = int(RetrieveDoubleParameter( ParameterFile , "innumbers"));
	outnumbers = int(RetrieveDoubleParameter( ParameterFile , "outnumbers"));
	InterPressure=zeros(Xnodes,Ynodes);
	CellColor = zeros(Xnodes, Ynodes);
	
	
	extern Matrix VisX, VisY; //viscosity
	extern Matrix ConductX, ConductY; //conductivity
	extern Matrix WallGradient;
	WallGradient=zeros(Xnodes, Ynodes);
	NodeLength = 1e-5;//*X(1) - *X(0);
	
	
	this->InitializeTime( VesselResume );
	//InitialAngiogenesisBackground(); //substituting DoSomeInitialisation()
	// this->InitializeVesselListInOrder( VesselResume );
	
	if(!VesselResume)
	{
		cout<<"ramdon"<<Injectionrandomized<<"in"<<innumbers<<"out"<<outnumbers<<endl;
		this->InjectPosition(innumbers, outnumbers, Injectionrandomized);
		cout<<"here after"<<endl;
	}
	
	
	
	this->InitializeVesselMatrix( VesselResume );
	
	int Iteration = 5000000;//5000000;
	double Tolerance = 0.0001;//0.01;
	
	double Pin, Pout;
	Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
	Pout = RetrieveDoubleParameter( ParameterFile , "Pout");
	
	
	ChainAngioPressureUpdate(Iteration, Tolerance); 
	cout<<"Check"<<endl; 
	
	GetVesselPressureAndFlow(Tolerance);
		//cout <<"function ~VesselInitialization finished"<<endl;	
}
void VesselStructure::InjectPosition(int innumbers, int outnumbers, bool randomized)
{
	
	
	
	extern int Xnodes;
	extern int Ynodes;
    int i = 0;
	int j = (Ynodes - 1)/2;
	int head = 90;
	int tail = 110;
	
	
	
	//cout<<"  2 ";
	if(!randomized)
	{
		VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
		
		int inflowX=0, inflowY=Ynodes-1;
		int outflowX=Xnodes-1, outflowY=Ynodes-1;
		
		/*
		 while(pCurrentVesselPoint)
		 {
		 if(pCurrentVesselPoint->i==head&&pCurrentVesselPoint->j==j)
		 {
		 pCurrentVesselPoint->AmIArtery=1;
		 }
		 else if(pCurrentVesselPoint->i==tail-1&&pCurrentVesselPoint->j==j)
		 {
		 pCurrentVesselPoint->AmIVenous=1;
		 }
		 pCurrentVesselPoint=pCurrentVesselPoint->pNextVesselPoint;
		 }
		 */
		
		
		while(pCurrentVesselPoint)
		{
			//if((pCurrentVesselPoint->i==0&&pCurrentVesselPoint->j==0)||(pCurrentVesselPoint->i==0&&pCurrentVesselPoint->j==50)||(pCurrentVesselPoint->i==0&&pCurrentVesselPoint->j==150)||(pCurrentVesselPoint->i==0&&pCurrentVesselPoint->j==200))
			if(pCurrentVesselPoint->j==0||pCurrentVesselPoint->i==0)
			//if(pCurrentVesselPoint->j==0&&pCurrentVesselPoint->i==12)
			{
				pCurrentVesselPoint->AmIArtery=1;
			}
			//else if((pCurrentVesselPoint->i==200&&pCurrentVesselPoint->j==0)||(pCurrentVesselPoint->i==200&&pCurrentVesselPoint->j==50)||(pCurrentVesselPoint->i==200&&pCurrentVesselPoint->j==150)||(pCurrentVesselPoint->i==200&&pCurrentVesselPoint->j==200))
			else if(pCurrentVesselPoint->j==200||pCurrentVesselPoint->i==200)
			{
				pCurrentVesselPoint->AmIVenous=1;
			}
			pCurrentVesselPoint=pCurrentVesselPoint->pNextVesselPoint;
		}
		
		
		
		
		
		
		
		
		
		
	}
	else
	{
		int i=0;
		cout<<"ramdon"<<randomized<<"in"<<innumbers<<"out"<<outnumbers<<endl;
		
		
		while(i<innumbers)
		{
			cout<<"ramdon"<<randomized<<"in"<<innumbers<<"out"<<outnumbers<<endl;
			
			
			int stop= int(( float( rand() ) / float( RAND_MAX))*VesselListLength);
			cout<<stop<<"   ";
			VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
			
			for(int j=0;j<stop;j++)
			{
				pCurrentVesselPoint=pCurrentVesselPoint->pNextVesselPoint;
			}
			if( pCurrentVesselPoint->AmIArtery!=1&&pCurrentVesselPoint->AmIVenous!=1)
			{
				pCurrentVesselPoint->AmIArtery=1;
				i++;
			}		
		}
		
		i=0;
		
		while(i<outnumbers)
		{
			
			int stop= int(( float( rand() ) / float( RAND_MAX))*VesselListLength);
			VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
			
			for(int j=0;j<stop;j++)
			{
				pCurrentVesselPoint=pCurrentVesselPoint->pNextVesselPoint;
			}
			if( pCurrentVesselPoint->AmIArtery!=1&&pCurrentVesselPoint->AmIVenous!=1)
			{
				pCurrentVesselPoint->AmIVenous=1;
				i++;
			}		
		}
		
		
		
		
	}//else end
	
}


void VesselStructure::InitializeTime(bool Resume)
{
 
//cout <<"function Initializetime running"<<endl;
 extern double t;
 FlowBegin = (bool) RetrieveDoubleParameter( ParameterFile , "FlowBegin");
 AdaptionBegin = (bool) RetrieveDoubleParameter( ParameterFile , "AdaptionBegin");
 if(t>0)
 {
	 FlowBegin=1;
	 AdaptionBegin=1;
 }
 if( Resume == 1)
 {
 extern char SaveDirectory[1025];
 char temp_string[1024];
 
	 sprintf( temp_string , "%s/angio_resume.dat" , SaveDirectory );
 ifstream angio_resume( temp_string, ios::in );
 angio_resume >> AngioDeltaT;
			 
 cout<<AngioDeltaT<<"   "<<endl;
            
 angio_resume.close();
 }
 else
 {
	AngioDeltaT = 0.05; //THIS IS WHERE I'M NOT SURE OF
	cout<<"AngioDeltaT"<<AngioDeltaT<<endl;
	

	
 }
//cout <<"function Initializetime finished"<<endl; 
}	
void VesselStructure::ArteryPosition()
{   
	//cout <<"function ArteryPosition running"<<endl; 
	
	
	
	extern int Xnodes;
	extern int Ynodes;
	extern Matrix VesselCells;
	extern Matrix SproutTips;
	extern Matrix MapIndicator;
	extern Matrix Tracer, TracerX, TracerY, TracerS, TracerT, Wss, Macrof;
	extern Matrix MapVesselCells;
	extern Matrix MapSproutTips;
    extern Matrix MapFlow;
    extern Matrix MapRadius;
	extern Matrix MapPressure;
	extern Matrix VesselAge;
	extern Matrix BloodVesselIndicator, BloodVesselIndicatorX,  BloodVesselIndicatorY;
    
	Wss = zeros(Xnodes, Ynodes);
	Tracer = zeros(Xnodes, Ynodes);
	TracerS = zeros(Xnodes, Ynodes);
	TracerX = zeros(Xnodes, Ynodes);
	TracerY = zeros(Xnodes, Ynodes);
	TracerT = zeros(Xnodes, Ynodes);
	Macrof = zeros(Xnodes, Ynodes);
	VesselCells = zeros(Xnodes, Ynodes);
	SproutTips = zeros(Xnodes, Ynodes);	
	MapIndicator = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapVesselCells =  zeros(2*Xnodes-1, 2*Ynodes-1);
	MapSproutTips = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapFlow = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapRadius = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapPressure = zeros(2*Xnodes-1, 2*Ynodes-1);
	VesselAge = zeros(Xnodes,Ynodes);	
	
	
	
	
	
	
    pFirstVesselPoint = new VesselPoint;
	pFirstVesselPoint->pPreviousVesselPoint = NULL;
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
	
	
	int i=0;
	int j=0;
	
			

	

	
	for(i=12;i<=187;i=i+25)
	{
	for(j=0;j<=200;j++)
	{
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
	}

	}
	
	
/****************************************************/
		
	pLastVesselPoint = pCurrentVesselPoint->pPreviousVesselPoint;
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	pLastVesselPoint -> pNextVesselPoint = NULL;
	//cout <<"function ArteryPosition finished"<<endl; 
	
}


	



void VesselStructure::InitializeVesselListInOrder(bool VesselResume)
{	
	//cout <<"function InitializeVesselListInOrder running"<<endl; 
	extern int Xnodes, Ynodes;
	extern bool AngiogenesisIsStarted;
	//extern Matrix BloodVesselIndicator;
	
    
	if (VesselResume == 0 && !AngiogenesisIsStarted)
	{
	
	//int InitialSprout = 5;
	SproutNumber = 0; 
	
		VesselPoint* pCurrentVesselPoint = pLastVesselPoint;
		
		
		int i=0;
		int j=0;
		
	while(i<=Xnodes-1)
	{
	
	if(i!=(Xnodes-1)/2)
	{
	j=0;
	while(j<=Ynodes-1)
	{	
		if(j%25!=0||j==(Ynodes-1)/2)//modified for special purpose
		{
	VesselPoint* pNewVesselPoint = new VesselPoint;
	pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
	pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
	pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
	
	
	//put the artery at the top
	//in out matrices, i goes from left to right as 0->1 
	//                 j goes form bottome to top as 0->1
	
	//put the sprout
	//for (int i=0; i< InitialSprout; i++)
    //{
	VesselListLength++;
	//SproutNumber++;
    //int Position = (int) floor( 1.0*(i + 0.5)*(Xnodes-1)/(InitialSprout) );//Min's Notes 1 :We should change here related to TAF
	
	pCurrentVesselPoint->i = i;
	pCurrentVesselPoint->j = j;
	}
	
	j++;
	
	}
	}
	i=i+25;
	}
   
   
  	
	
	pLastVesselPoint = pCurrentVesselPoint;
	//pLastVesselPoint = pCurrentVesselPoint->pPreviousVesselPoint;	
	//pLastVesselPoint -> pNextVesselPoint = NULL;
    }
	else
	{
	
 extern char SaveDirectory [1025];
 char temp_string [1024];
 sprintf( temp_string , "%s/pVesselList_resume.dat" , SaveDirectory );
 ifstream pVesselList_resume( temp_string, ios::in ); 
	pFirstVesselPoint = new VesselPoint;
	pFirstVesselPoint->pPreviousVesselPoint = NULL;
    VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
	pVesselList_resume >> VesselListLength;
	pVesselList_resume >> SproutNumber;
	
	
		
	 

		

	
 for(int i=0;i<VesselListLength; i++)
 {
    		
	pVesselList_resume >> pCurrentVesselPoint->i;
	pVesselList_resume >> pCurrentVesselPoint->j;
	pVesselList_resume >> pCurrentVesselPoint->AmIArtery ;
	pVesselList_resume >> pCurrentVesselPoint->AmIVenous ;

 
	VesselPoint* pNewVesselPoint = new VesselPoint;
	pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
	pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
	pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
	
}
 pCurrentVesselPoint = pCurrentVesselPoint->pPreviousVesselPoint;
	delete pCurrentVesselPoint->pNextVesselPoint;
	pCurrentVesselPoint->pNextVesselPoint=NULL;
	pLastVesselPoint = pCurrentVesselPoint;

	
	
	/**
	 extern char SaveDirectory [1025];
 char temp_string [1024];
 sprintf( temp_string , "%s/pVesselList_resume.dat" , SaveDirectory );
 ifstream pVesselList_resume( temp_string, ios::in ); 
	pFirstVesselPoint = new VesselPoint;
	pFirstVesselPoint->pPreviousVesselPoint = NULL;
    VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
	pVesselList_resume >> VesselListLength;
	pVesselList_resume >> SproutNumber;
	
	VesselListLength=0;
	
	for(int i=0;i<Xnodes;i++)
	{
		for(int j=0;j<Ynodes;j++)
		{
		
		if(*BloodVesselIndicator(i,j)>0)
		{			
	
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
	pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
	pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
	VesselListLength++;
			
		}
		}
	
	}
	
	VesselListLength--;
	pCurrentVesselPoint = pCurrentVesselPoint->pPreviousVesselPoint;
	delete pCurrentVesselPoint->pNextVesselPoint;
	pCurrentVesselPoint->pNextVesselPoint=NULL;
	pLastVesselPoint = pCurrentVesselPoint;
	**/
	
	}
	//cout <<"function InitializeVesselListInOrder finished"<<endl; 

	
}

// Update SproutTips, BloodVesselIndicator, DirectionX, DirectionY
void VesselStructure::InitializeVesselMatrix(bool VesselResume)
{
	
	cout <<"function InitializeVesselMatrix running"<<endl; 
	double Rmin, Rmax, mu_blood;
	double pi = 3.141592654;
	int div=25;
	Rmin = RetrieveDoubleParameter( ParameterFile , "Rmin");
	Rmax = RetrieveDoubleParameter( ParameterFile , "Rmax");
	mu_blood = RetrieveDoubleParameter( ParameterFile , "mu_blood");
	
	MaxWss = RetrieveDoubleParameter( ParameterFile , "MaxWss");
	if( MaxWss < 1e-5 )
	{ MaxWss = 2.0; }
	double ArteryRadius;
	ArteryRadius = RetrieveDoubleParameter( ParameterFile , "ArteryRadius");
	
	
	extern bool AngiogenesisIsStarted;
	extern int Xnodes, Ynodes;
	
	extern Matrix BloodVesselIndicator;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix BloodVesselRadius;
	extern Matrix Stability;
	extern Matrix PreExist;
	//mextern Matrix BloodVesselFlowRate;
	extern Matrix HaematocritLevel;
	extern Matrix SproutTips;
	extern Matrix SproutAge;
	extern Matrix VesselAge;
	extern Matrix BloodVesselRadiusXdir;
	extern Matrix BloodVesselRadiusYdir;
	extern Matrix BloodNodePressure;
	extern Matrix BloodVesselPressureX;
	extern Matrix BloodVesselPressureY;
	//extern Matrix DirectionX, DirectionY;
	extern Matrix FlowX, FlowY;
	extern Matrix WssX, WssY;
	
	extern Matrix VisX, VisY; //viscosity
	extern Matrix ConductX, ConductY; //conductivity
	double RandNumber;
	
	if (VesselResume == 0 && !AngiogenesisIsStarted)
	{
		
		BloodVesselIndicator = zeros( Xnodes , Ynodes );
		BloodVesselIndicatorX = zeros( Xnodes , Ynodes );
		BloodVesselIndicatorY = zeros( Xnodes , Ynodes );
		HaematocritLevel = zeros( Xnodes , Ynodes );
		SproutTips = zeros( Xnodes , Ynodes );
		SproutAge = zeros(Xnodes, Ynodes);
		VesselAge = zeros(Xnodes, Ynodes);
		BloodVesselRadiusXdir = zeros(Xnodes , Ynodes);
		BloodVesselRadiusYdir = zeros(Xnodes , Ynodes);
		for(int i = 0; i< Xnodes; i++)
		{
			for(int j = 0; j< Ynodes; j++)
			{
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusXdir(i, j) = Rmin + RandNumber*(Rmax - Rmin);
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusYdir(i, j) = Rmin + RandNumber*(Rmax - Rmin);
			}
			
		}
		BloodVesselRadius = zeros( Xnodes , Ynodes );
		BloodNodePressure = zeros( Xnodes, Ynodes );
		BloodVesselPressureX = zeros( Xnodes, Ynodes );
		BloodVesselPressureY = zeros( Xnodes, Ynodes );
		Stability = zeros(Xnodes, Ynodes);
		PreExist = zeros(Xnodes, Ynodes);
		//Direction Matrix only matters to Sprouts
		//DirectionX = zeros( Xnodes, Ynodes ); // 0 means no vessel, 1, -1 means one way direction
		//DirectionY = zeros( Xnodes, Ynodes );
		
		FlowX = zeros( Xnodes, Ynodes );
		FlowY = zeros( Xnodes, Ynodes );
		WssX = zeros( Xnodes, Ynodes );
		WssY = zeros( Xnodes, Ynodes );
		
		VisX = ones( Xnodes, Ynodes );
		VisY = ones( Xnodes, Ynodes );
		ConductX = zeros( Xnodes, Ynodes );
		ConductY = zeros( Xnodes, Ynodes );
		VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
		
	

	int i=0;
	int j=0;
	
	/*************************1*************************************/		

		for(i=12;i<=187;i=i+25)
		{
		
		 j=0;
		 
		*BloodVesselIndicatorY(i, j) = 1;
		*BloodVesselIndicator(i, j) = 1;
		*VisY(i, j) = 0;
		*ConductY(i, j) = 0;		
		 for(j=1;j<200;j++)
		{
			    *BloodVesselIndicatorY(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				*VisY(i, j) = mu_blood;				
				*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);	
		}
		
		
		
		
		*BloodVesselIndicatorY(i, j) = -1;
		*BloodVesselIndicator(i, j) = 1;
		*VisY(i, j) = mu_blood;		
		*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);		
		
        }
		
		
		
		
	}
	else if (VesselResume == 1 && !AngiogenesisIsStarted)
		
	{
		
		FlowX = zeros( Xnodes, Ynodes );
		FlowY = zeros( Xnodes, Ynodes );
		WssX = zeros( Xnodes, Ynodes );
		WssY = zeros( Xnodes, Ynodes );
		
		VisX = ones( Xnodes, Ynodes );
		VisY = ones( Xnodes, Ynodes );
		ConductX=zeros(Xnodes,Ynodes);
		ConductY=zeros(Xnodes,Ynodes);
		//for (int i=0; i< Xnodes; i++)
		//{
		//	for(int j=0; j<Ynodes; j++)
		//	{
		//	*ConductX(i, j) = 1e-25;
		//	*ConductY(i, j) = 1e-25;
		//cout<<*ConductX(i, j);
		//	}
		//}
		BloodNodePressure = zeros( Xnodes, Ynodes );
		UpdateViscosity();
		UpdateConductivity();
		//add things here
	}
	
	
	
	
	
	cout <<"function InitializeVesselMatrix finished"<<endl; 
}


void VesselStructure::GetRadiusValue()
{
//cout <<"function GetRadiusValue running"<<endl; 
extern Matrix BloodVesselRadiusXdir;
extern Matrix BloodVesselRadiusYdir;
extern Matrix BloodVesselRadius;
extern Matrix BloodVesselIndicator;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;

extern int Xnodes, Ynodes;
BloodVesselRadius = zeros(Xnodes, Ynodes);
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
while(pCurrentVesselPoint)
{
int i= pCurrentVesselPoint->i;
int j= pCurrentVesselPoint->j;
	if(i!=(Xnodes-1)&&j!=(Ynodes-1)&&i!=0&&j!=0)//don't consider the boundary
		{
			*BloodVesselRadius(i,j) = 0;	
			
			if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
				{
					*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i,j);
					*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i+1,j);
					
				}
			else if(*BloodVesselIndicatorX(i,j)>0.5)
				{
					*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i+1,j);
				}
			else if(*BloodVesselIndicatorX(i,j)<-0.5)
				{
					*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i,j);
				}


			if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
				{
					*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j);
					*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j+1);
					
				}
			else if(*BloodVesselIndicatorY(i,j)>0.5)
				{
					*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j+1);
				}
			else if(*BloodVesselIndicatorY(i,j)<-0.5)
				{
					*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j);
				}





		*BloodVesselRadius(i,j) 
			/=(min_abs(*BloodVesselIndicatorX(i,j))+min_abs(*BloodVesselIndicatorY(i,j)));

		}
pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
}

//cout <<"function GetRadiusValue finished"<<endl; 
}






bool VesselStructure::SaveVesselList()
{
//cout <<"function SaveListInOrder running"<<endl; 

extern bool AngiogenesisIsStarted;
extern bool AngiogenesisDisabled;

if( AngiogenesisIsStarted == false ||
     AngiogenesisDisabled == true )
 { 
 return false; 
 }	
 
 extern char SaveDirectory[1025];
 char temp_string[1024];
 
 sprintf( temp_string, "%s/angio_resume.dat", SaveDirectory);
 ofstream angio_resume( temp_string, ios::out );
 angio_resume << AngioDeltaT <<"\t"
			  
              		  
              << endl;	
cout<<AngioDeltaT<<" save  "<<endl;				  
 angio_resume.close();	
 
 
 //save list now
  sprintf( temp_string , "%s/pVesselList_resume.dat" , SaveDirectory );
 ofstream pVesselList_resume( temp_string, ios::out );
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 pVesselList_resume << VesselListLength <<"\t";
 pVesselList_resume << SproutNumber<<"\t";
 while(pCurrentVesselPoint)
 {
 pVesselList_resume << pCurrentVesselPoint->i<<"\t"
				    << pCurrentVesselPoint->j<<"\t"
					<< pCurrentVesselPoint->AmIArtery<<"\t"
					<< pCurrentVesselPoint->AmIVenous<<"\t";
					
 pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;					
 }
 pVesselList_resume.close();
// cout <<"function SaveListInOrder finished"<<endl;
 return true;
 }
 
 
 
void VesselStructure::ChainAngioPressureUpdate(int MaxIterations, double Tolerance)
{
	extern Matrix ConductX,ConductY;
	extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
	extern Matrix BloodNodePressure;
	extern Matrix X,Y;
	extern int Xnodes, Ynodes;
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;//initialize the updating point
	double Error=2*Tolerance;
	double MaxError=2*Tolerance;
	int Iterations=0;
	double NewValue=0.0;
	int i=0;
	int j=0;
	double Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
	double Pout = RetrieveDoubleParameter( ParameterFile , "Pout");
	double MaxConduct= 1e-25;
	for(int ii=0; ii<X.TellCols(); ii++)
	{
		for(int jj=0; jj<Y.TellCols(); jj++)
		{
			//cout<<*BloodVesselIndicatorX(ii,jj)<<endl;
			if(*ConductX(ii,jj)>MaxConduct)
			{MaxConduct=*ConductX(ii,jj);}
			if(*ConductY(ii,jj)>MaxConduct)
			{MaxConduct=*ConductY(ii,jj);}
		}
	}
	//BloodNodePressure=zeros(Xnodes,Ynodes);		
	
	while(MaxError>Tolerance&&Iterations<MaxIterations)
	{
		//relocalize the point
		pCurrentVesselPoint = pFirstVesselPoint;
		MaxError=0;
		//sweep along the chain
		while(pCurrentVesselPoint)
		{
			i=pCurrentVesselPoint->i;
			j=pCurrentVesselPoint->j;
			//if(i==0)
			//{
			//NewValue = Pin;
			//}
			
			
			if(pCurrentVesselPoint->AmIArtery)
			{
				NewValue = Pin;
			}
			else if(pCurrentVesselPoint->AmIVenous)
			{
				NewValue = Pout;
			}
	
			
			else
			{
				NewValue = AngioSORUpdate( BloodNodePressure, ConductX, ConductY, BloodVesselIndicatorX, BloodVesselIndicatorY,i, j, MaxConduct);
				// if((min_abs(NewValue)>Pin))
				// {cout<<*BloodVesselIndicatorX(i,j)<<"   "<<*BloodVesselIndicatorY(i,j)<<"   "<<i<<"  ,  "<<j<<"    "<<NewValue<<"   ";}
				//cout<<"NewValue:  "<<NewValue<<endl;
			}	 
			Error = min_abs( NewValue - *BloodNodePressure(i,j) );
			if( Error > MaxError )
			{ MaxError = Error; }
			
			*BloodNodePressure(i,j) = NewValue;
			
			pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//Move on
		}
		Iterations++;
		if( Iterations % 1000 == 0 )
		{ cout << Iterations << " : " << MaxError << endl; }  
	}
	cout << "summary:: iterations: " << Iterations << endl
	<< "          error     : " << MaxError << endl << endl; 
	
	
}




void VesselStructure::GetVesselPressureAndFlow(double Tolerance)
{
cout <<"function GetVesselPressure running"<<endl; 
// extern Matrix DirectionX, DirectionY;
 extern Matrix BloodVesselIndicatorX;
 extern Matrix BloodVesselIndicatorY;
 extern Matrix BloodVesselPressureX;
 extern Matrix BloodVesselPressureY;
 extern Matrix BloodNodePressure;
 extern Matrix InterPressure;
 extern Matrix WallGradient;
 extern Matrix FlowX, FlowY;
 extern Matrix CollapseClock;
 extern Matrix ConductX, ConductY;

 extern int Xnodes, Ynodes;
 //double MinConduct=1000;
double Pin, Pout;
Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
Pout = RetrieveDoubleParameter( ParameterFile , "Pout");


 BloodVesselPressureX = zeros(Xnodes, Ynodes);
 BloodVesselPressureY = zeros(Xnodes, Ynodes);
 
 
 

 
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
while(pCurrentVesselPoint)
{
int i = pCurrentVesselPoint->i;
int j = pCurrentVesselPoint->j;
*WallGradient(i,j) = *BloodNodePressure(i,j)-*InterPressure(i,j);
	
	
//X direction
if(i == 0)//boundary
{
	

 if(*BloodVesselIndicatorX(i,j)>0.5)
{
*FlowX(i, j) = 0;//( *BloodNodePressure(i, j) - *BloodNodePressure(i-1, j) ) * *ConductX(i, j);

}

}

else if(i == Xnodes - 1 )//boundary
{
	
	 if(*BloodVesselIndicatorX(i,j)<-0.5)
{
*FlowX(i, j) = ( *BloodNodePressure(i-1, j) - *BloodNodePressure(i, j) ) * *ConductX(i, j);
	if(*CollapseClock(i,j)>0)
	{
		*FlowX(i,j)	= 0;
		
	}
	if(*CollapseClock(i-1,j)>0)
	{
		*FlowX(i,j) = 0;	
	}
		
*BloodVesselPressureX(i,j)=(*BloodNodePressure(i-1,j)+*BloodNodePressure(i,j))/2;
}
}

else
{
	
if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)//when the point is fully connected
{
*FlowX(i, j) = ( *BloodNodePressure(i-1, j) - *BloodNodePressure(i, j) ) * *ConductX(i, j);
	if(*CollapseClock(i,j)>0)
	{
		*FlowX(i,j)	= 0;
		
	}
	if(*CollapseClock(i-1,j)>0)
	{
		*FlowX(i,j) = 0;	
	}
*BloodVesselPressureX(i,j)=(*BloodNodePressure(i-1,j)+*BloodNodePressure(i,j))/2;
}
	
else if(*BloodVesselIndicatorX(i,j)<-0.5)
{
*FlowX(i, j) = ( *BloodNodePressure(i-1, j) - *BloodNodePressure(i, j) ) * *ConductX(i, j);
	if(*CollapseClock(i,j)>0)
	{
		*FlowX(i,j)	= 0;
		
	}
	if(*CollapseClock(i-1,j)>0)
	{
		*FlowX(i,j) = 0;	
	}
*BloodVesselPressureX(i,j)=(*BloodNodePressure(i-1,j)+*BloodNodePressure(i,j))/2;
}

else if(*BloodVesselIndicatorX(i,j)>0.5)
{
*FlowX(i, j) = 0.0;//( *BloodNodePressure(i, j) - *BloodNodePressure(i+1, j) ) * *ConductX(i, j);

}


}

// Y direction

if(j == 0)//boundary
{
	

 if(*BloodVesselIndicatorY(i,j)>0.5)
{
*FlowY(i, j) = 0;//( *BloodNodePressure(i, j) - *BloodNodePressure(i+1, j) ) * *ConductY(i, j);

}

}

else if(j == Ynodes - 1 )//boundary
{
	
	 if(*BloodVesselIndicatorY(i,j)<-0.5)
{
*FlowY(i, j) = ( *BloodNodePressure(i, j-1) - *BloodNodePressure(i, j) ) * *ConductY(i, j);
	if(*CollapseClock(i,j)>0)
	{
		*FlowY(i,j)	= 0;
		
	}
	if(*CollapseClock(i,j-1)>0)
	{
		*FlowY(i,j) = 0;	
	}
*BloodVesselPressureY(i,j)=(*BloodNodePressure(i,j-1)+*BloodNodePressure(i,j))/2;
}

}

else
{
	
if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)//when the point is fully connected
{
*FlowY(i, j) = ( *BloodNodePressure(i, j-1) - *BloodNodePressure(i, j) ) * *ConductY(i, j);
	if(*CollapseClock(i,j)>0)
	{
		*FlowY(i,j)	= 0;
		
	}
	if(*CollapseClock(i,j-1)>0)
	{
		*FlowY(i,j) = 0;	
	}
*BloodVesselPressureY(i,j)=(*BloodNodePressure(i,j-1)+*BloodNodePressure(i,j))/2;
}
	
else if(*BloodVesselIndicatorY(i,j)<-0.5)
{
*FlowY(i, j) = ( *BloodNodePressure(i, j-1) - *BloodNodePressure(i, j) ) * *ConductY(i, j);
	if(*CollapseClock(i,j)>0)
	{
		*FlowY(i,j)	= 0;
		
	}
	if(*CollapseClock(i,j-1)>0)
	{
		*FlowY(i,j) = 0;	
	}
*BloodVesselPressureY(i,j)=(*BloodNodePressure(i,j-1)+*BloodNodePressure(i,j))/2;
}

else if(*BloodVesselIndicatorY(i,j)>0.5)
{
*FlowY(i, j) = 0;//( *BloodNodePressure(i, j) - *BloodNodePressure(i, j+1) ) * *ConductY(i, j);
}

}

if(*ConductX(i,j)!=0)
{
if(min_abs(*FlowX(i,j)/ *ConductX(i,j))<Tolerance)
{
*FlowX(i,j) = 0;
}
}

if(*ConductY(i,j)!=0)
{
if(min_abs(*FlowY(i,j)/ *ConductY(i,j))<Tolerance)
{
*FlowY(i,j) = 0;

}
}
	
	
//cout<<*BloodNodePressure(i, j)<<endl;


	
	
	

pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;

}






cout <<"function GetVesselPressure finished"<<endl; 

}


double AngioSORUpdate( Matrix& Solution, Matrix& ConductX, Matrix& ConductY, Matrix& BloodVesselIndicatorX, Matrix& BloodVesselIndicatorY, int i, int j, double MaxConduct)
{
//cout<<"AngioImplicitUpdate"<<endl;
 double dt = 0.8/MaxConduct;
 
 extern double t;
 extern Matrix X;
 extern int Xnodes,Ynodes;
// extern Matrix DirectionX,DirectionY;
 double Length=1e-15;
 double OutPut = 0.0;
 double CoeLeak = 0.0;
// extern Matrix BloodVesselIndicatorX;
// extern Matrix BloodVesselIndicatorY;//So, BloodVessel is also important.
 //cout<<MaxConduct<<"MaxConduct"<<endl;
 double Uij = *Solution(i,j);
 double Ui_jp1 = 0;
 double Ui_jm1 = 0;
 double Uim1_j = 0;
 double Uip1_j = 0;

 double CoeffYell = 0;
 double CoeffYr = 0;
 double CoeffXell = 0;
 double CoeffXr   = 0; 

  if(min_abs(*BloodVesselIndicatorX(i,j))+min_abs(*BloodVesselIndicatorY(i,j))>0.5)
  {
  ////////
  if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)	
  {
	  if(i==0)
	  {
	  CoeffXell =0;
	  
	  }
	  else
	  {
	  CoeffXell = *ConductX(i,j);//(*ConductX(i,j)+*ConductX(i-1,j))/2 ;
	  }
	  
	  if(i==Xnodes-1)
	  {
	  CoeffXr   = 0 ;
	  }
	  else
	  {
	  CoeffXr   = *ConductX(i+1,j) ;
	  }
	  if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
	  {
	   if(j==0)
	   {
	   CoeffYell = 0;//(*ConductY(i,j)+*ConductY(i,j))/2 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   CoeffYr   = *ConductY(i,j+1);
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j);//(*ConductY(i,j)+*ConductY(i,j))/2 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   CoeffYr   = 0;
	   }
	   else
	   {
	   CoeffYell = *ConductY(i,j);
	   CoeffYr   = *ConductY(i,j+1);
	   }
	  }
	  else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
	  {
	  
	  }
	  else if(*BloodVesselIndicatorY(i,j)>0)
	  {
	  	if(j==0)
	   {
	   
	   CoeffYr   = *ConductY(i,j+1);
	   }
	   else if(j==Ynodes-1)
	   {
	   
	   CoeffYr   = 0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   
	   CoeffYr   = *ConductY(i,j+1); 
	   }
	  }
	  else
	  {
	  	if(j==0)
	   {
	   CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j);
	   
	   }
	   else
	   {
	   CoeffYell =  *ConductY(i,j);	  
	   }
	  }
  }
  else if(min_abs(*BloodVesselIndicatorX(i,j))<0.5)
  {
  	  if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
	  {
	   if(j==0)
	   {
	   CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   CoeffYr   = *ConductY(i,j+1) ;
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j) ;
	   CoeffYr   = 0 ;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   CoeffYell =  *ConductY(i,j) ;
	   CoeffYr   = *ConductY(i,j+1) ;
	   }
	  }
	  else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
	  {
	  
	  }
	  else if(*BloodVesselIndicatorY(i,j)>0)
	  {
	  	if(j==0)
	   {
	   
	   CoeffYr   = *ConductY(i,j+1);
	   }
	   else if(j==Ynodes-1)
	   {
	   
	   CoeffYr   = 0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   
	   CoeffYr   =*ConductY(i,j+1); 
	   }
	  }
	  else
	  {
	  	if(j==0)
	   {
	   CoeffYell = 0;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j);
	   
	   }
	   else
	   {
	    CoeffYell = *ConductY(i,j);
	  
	   }
	  }
  }
  else if(*BloodVesselIndicatorX(i,j)>0)
  {

	  if(i==Xnodes-1)
	  {
	  CoeffXr   = 0 ;
	  }
	  else
	  {
	  CoeffXr   = *ConductX(i+1,j); 
	  }
      
       if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
	  {
	   if(j==0)
	   {
	   CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   CoeffYr   = *ConductY(i,j+1);
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j) ;
	   CoeffYr   =0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   CoeffYell = *ConductY(i,j) ;
	   CoeffYr   = *ConductY(i,j+1) ; 
	   }
	  }
	  else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
	  {
	  
	  }
	  else if(*BloodVesselIndicatorY(i,j)>0)
	  {
	  	if(j==0)
	   {
	   
	   CoeffYr   = *ConductY(i,j+1) ;
	   }
	   else if(j==Ynodes-1)
	   {
	   
	   CoeffYr   = 0 ;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   
	   CoeffYr   = *ConductY(i,j+1) ; 
	   }
	  }
	  else
	  {
	  	if(j==0)
	   {
	   CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j) ;
	   
	   }
	   else
	   {
	   CoeffYell = *ConductY(i,j) ;
	  
	   }
	  }
  }
  else
  {
        if(i==0)
	  {
	  CoeffXell =0;
	  }
	  else
	  {
	  CoeffXell = *ConductX(i,j) ;
	  }

	
       if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
	  {
	   if(j==0)
	   {
	   CoeffYell = 0;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   CoeffYr   = *ConductY(i,j+1) ;
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j) ;
	   CoeffYr   = 0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   CoeffYell = *ConductY(i,j) ;
	   CoeffYr   = *ConductY(i,j+1) ; 
	   }
	  }
	  else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
	  {
	  
	  }
	  else if(*BloodVesselIndicatorY(i,j)>0)
	  {
	  	if(j==0)
	   {
	   
	   CoeffYr   = *ConductY(i,j+1);
	   }
	   else if(j==Ynodes-1)
	   {
	   
	   CoeffYr   = 0 ;//(*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   
	   CoeffYr   = *ConductY(i,j+1) ; 
	   }
	  }
	  else
	  {
	  	if(j==0)
	   {
	   CoeffYell = 0;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j);
	   
	   }
	   else
	   {
	   CoeffYell = *ConductY(i,j);
	  
	   }
	  }
  }
   
	
	 ////// 
	 if(i==0)
     {
		Uim1_j = *Solution(i,j); 
	 }
   else
     {
		Uim1_j = *Solution(i-1,j);  
     }
   if(i==Xnodes-1)
     {
		Uip1_j = *Solution(i,j);
     }
   else
     {
		Uip1_j = *Solution(i+1,j);
     }

	 
	 
   if(j==0)
     {
		Ui_jm1 = *Solution(i,j); //Ui_jm1 = *Solution(i,Ynodes-1);
	 }
   else
     {
		Ui_jm1 = *Solution(i,j-1);  
     }
   if(j==Ynodes-1)
     {
		Ui_jp1 = *Solution(i,j);//Ui_jp1 = *Solution(i,0);
     }
   else
     {
		Ui_jp1 = *Solution(i,j+1);
     }
	
	 
	//if(j!=Ynodes-1)
	//{
	//  cout<<i<<","<<j<<*BloodVesselIndicatorX(i,j)<<"  "<<*BloodVesselIndicatorY(i,j)<<endl;
	//  cout<<"CoeffXell"<<CoeffXell<<"CoeffXr"<<CoeffXr<<endl;
	  
	 // cout<<"CoeffYell"<<CoeffYell<<"CoeffYr"<<CoeffYr<<endl;
	 
	
	//}  
	//OutPut =  ((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr) );
 OutPut =  1.85*((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr) )-0.85*Uij;   
 if(!(min_abs(OutPut)<100000000.0))
 {
 cout<<OutPut<<"i  "<<i<<"j   "<<j<<" Uim1_j "<< Uim1_j<<" Uip1_j "<< Uip1_j<<" Ui_jm1 "<< Ui_jm1<<" Ui_jp1 "<< Ui_jp1<<endl;
 cout<<" left "<< CoeffXell<<" right "<< CoeffXr<<" down "<<CoeffYell<<" up "<<CoeffYr; 
 getchar();
 }	 
  
  
  return OutPut;
  }
  else// single point
  {
  return Uij;
  }
   
}


void VesselStructure::UpdateVesselSystemSteadyStateShuffle(CellVesselStructure* TheVessel)
{
	cout <<"function UpdateVesselSystemSteadyState by pressure running"<<endl; 
	extern int Xnodes, Ynodes;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix LymIndicator;
	extern Matrix InterPressure;
	extern Matrix BloodVesselRadius;
	//extern Matrix BloodVesselFlowRate;
	extern Matrix SproutTips;
	extern Matrix SproutAge;
	extern Matrix VesselAge;
	extern Matrix Macrof;
	extern Matrix BloodVesselRadiusXdir;
	extern Matrix BloodVesselRadiusYdir;
	extern Matrix BloodNodePressure;
	extern Matrix BloodVesselPressureX;
	extern Matrix BloodVesselPressureY;
	// extern Matrix DirectionX, DirectionY;
	extern Matrix FlowX, FlowY;
	extern Matrix TAF;
	extern Matrix WssX, WssY;
	extern Matrix VisX, VisY; //viscosity
	extern Matrix ConductX, ConductY; //conductivity
	extern double GrowthDuration;
	extern double FlowDuration;
	//extern bool ResetPressure;
	extern double t;
	int i=0;
	LymIndicator=zeros(Xnodes, Ynodes);
	 extern int innumbers, outnumbers;
     extern bool Injectionrandomized;
	
	//cout << "UVSSSS begin, flowbegin: " << FlowBegin << endl;
	//getchar();
	
	if( SproutNumber == 0 )
	{
		cout << "Warning: All sprouts are dead! " << endl << endl;
	}
	cout << "sprout count: " << SproutNumber << endl;
	//cout << "flowbegin = " << FlowBegin << endl;
	if(!FlowBegin)
	{
		
		//Remodel(TheVessel);
		GetMaxTAF();
		TheVessel->ShuffleFate(TAF);
		TheVessel->ColorCell();
		TheVessel->SetSprout(TAF);
		GrowSprout_onecell(TheVessel);
		GrowSprout_onecell(TheVessel);
		GrowSprout_onecell(TheVessel);
		GrowSprout_onecell(TheVessel);
		Retract(TheVessel);
		Retract(TheVessel);
		Retract(TheVessel);
		

		//GrowSprout(TheVessel);
		//AngioUpdateMDE();
		//AngioUpdateECM();
		//AngioUpdateTAF();
		// GetMaxTAF();
		//cout << "I am starting again, haha" << endl;
		//WSSTAFBranchByProbInterval(Shuffle);
		TransOutPut();
		// RegenerateHem();
		MakeList();
		
	}
	else
	{
	    //Remodel(TheVessel);
	    UpdateStability();
		GetMaxTAF();
		TheVessel->MemoShuffleFate(TAF);
		TheVessel->ColorCell();
		TheVessel->MemoSetSprout(TAF);
		
		//AngioUpdateTAF();
		if(t==0.05)
		{
		InitialGrowSprout(TheVessel);
		InjectPosition(innumbers, outnumbers, Injectionrandomized);
		}
		else
		{
		GrowSprout_Step(TheVessel,1);
		//GrowSprout_onecell(TheVessel);
		//GrowSprout_onecell(TheVessel);
		//GrowSprout_onecell(TheVessel);
		}
		//GrowSprout(TheVessel);
		//AngioUpdateMDE();
		//AngioUpdateECM();
		Remove(TheVessel);
		
		Retract(TheVessel);
		Retract(TheVessel);
		Retract(TheVessel);
		Retract(TheVessel);
		Retract(TheVessel);
		Retract(TheVessel);
		Retract(TheVessel);
		Retract(TheVessel);
		Retract(TheVessel);
		
		
		//WSSTAFBranchByProbInterval(Shuffle);
		
		
		TransOutPut();
		VesselAdaption();
		
		Remove(TheVessel);
		TransOutPut();
		// RegenerateHem();
		
		MakeList();
				
		//cout<<*Macrof(100,100);
		//getchar();
		MoveMCell(1, 0.5);
		RegenerateFlow();//SHIYISHI
		
	}
	
	
	
	cout<<"Length:"<<VesselListLength<<endl;
	cout <<"function UpdateVesselSystemSteadyState by pressure finished"<<endl; 
}




void AngioUpdateMDE(void)
{

//cout <<"function AngioUpdateMDE running"<<endl; 
extern Matrix MDE;
extern Matrix SproutTips;

extern int Xnodes, Ynodes;
double AngiogenesisAlpha = RetrieveDoubleParameter( ParameterFile, "AngiogenesisAlpha");
for (int j = 0; j < Ynodes; j++)
 {
for(int i = 0; i < Xnodes; i++)
  {
if(*SproutTips(i,j) > 0) 
   {
    *MDE(i,j) = *MDE(i,j) + AngioDeltaT * AngiogenesisAlpha * *SproutTips(i,j);
   }
  }
 }
//cout <<"function AngioUpdateMDE finished"<<endl; 
}

void AngioUpdateECM(void)
{
//cout <<"function AngioUpdateECM running"<<endl; 
extern Matrix ECM;
extern Matrix MDE;
extern Matrix SproutTips;
extern double ECM_DegradationConstant;

extern int Xnodes, Ynodes;
double beta= RetrieveDoubleParameter( ParameterFile , "beta"); // 0.05;
double gamma= RetrieveDoubleParameter( ParameterFile , "gamma");
for (int j = 0; j < Ynodes; j++)
 {
for(int i = 0; i < Xnodes; i++)
  {
  
   *ECM(i,j) = *ECM(i,j) - AngioDeltaT * gamma * *ECM(i,j) * *MDE(i,j);
if(*SproutTips(i,j) > 0) 
   {
   *ECM(i,j) = *ECM(i,j) + AngioDeltaT * beta * *SproutTips(i,j);
	//*ECM(i,j) = *ECM(i,j) + AngioDeltaT * ( beta -  ECM_DegradationConstant * *ECM(i, j) ) ;
   }
   
  }
 }
// //cout <<"function AngioUpdateECM finished"<<endl; 

}

void AngioUpdateTAF(void)
{
//cout <<"function AngioUpdateECM running"<<endl; 
extern Matrix TAF;
extern Matrix BloodVesselIndicator;
extern double EndothelialTAFuptakeRate;

extern int Xnodes, Ynodes;
for (int j = 0; j < Ynodes; j++)
 {
for(int i = 0; i < Xnodes; i++)
  {
  
if(*BloodVesselIndicator(i,j) > 0) 
   {
    //COMBINE *ECM(i,j) = *ECM(i,j) + AngioDeltaT * beta * *SproutTips(i,j);
	*TAF(i,j) = *TAF(i,j) - *BloodVesselIndicator(i,j)*AngioDeltaT * EndothelialTAFuptakeRate;
   }
   if(*TAF(i,j)<=0)
   {
   *TAF(i,j) = 0;
   }
   
  }
 }
// //cout <<"function AngioUpdateECM finished"<<endl; 

}

void GetMaxTAF()
{
	//cout <<"function GetMaxTAF running"<<endl; 

	extern Matrix TAF;
	
	MaxTAF = 1;
	extern int Xnodes;
	extern int Ynodes;
	/*
	for(int i = 0; i< Xnodes; i++)
	{
		for(int j = 0; j< Ynodes; j++)
		{
			if (*TAF(i, j) > MaxTAF)
			{
				MaxTAF = *TAF(i, j);
			}
		}
	}
	//cout <<"function GetMaxTAF finished"<<endl; 
*/

}
void VesselStructure::GrowSprout(CellVesselStructure* TheVessel)
{
	cout <<"function GrowSprout running"<<endl; 
	extern bool VesselBroke;
	extern int touchpoints;
	extern Matrix SproutTips;
	extern Matrix SproutAge;
	extern Matrix VesselAge;
	//extern Matrix DirectionX;
	//extern Matrix DirectionY;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix BloodVesselIndicator;
	extern Matrix TAF;
	extern Matrix ECM;
	extern Matrix CellColor;
	extern int Xnodes, Ynodes;
	double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
	double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
	double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
	double String = RetrieveDoubleParameter( ParameterFile , "String");
	double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
	AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
	AngioDeltaY = 1.0 /Ynodes;
	Matrix Prob0 = zeros(Xnodes, Ynodes);
	Matrix Prob1 = zeros(Xnodes, Ynodes);
	Matrix Prob2 = zeros(Xnodes, Ynodes);
	Matrix Prob3 = zeros(Xnodes, Ynodes);
	Matrix Prob4 = zeros(Xnodes, Ynodes);
	Matrix Inf0 = zeros(Xnodes, Ynodes);
	Matrix Inf1 = zeros(Xnodes, Ynodes);
	Matrix Inf2 = zeros(Xnodes, Ynodes);
	Matrix Inf3 = zeros(Xnodes, Ynodes);
	Matrix Inf4 = zeros(Xnodes, Ynodes);
	Matrix Sup0 = zeros(Xnodes, Ynodes);
	Matrix Sup1 = zeros(Xnodes, Ynodes);
	Matrix Sup2 = zeros(Xnodes, Ynodes);
	Matrix Sup3 = zeros(Xnodes, Ynodes);
	Matrix Sup4 = zeros(Xnodes, Ynodes);
	int iminus,iplus,jminus,jplus;
	bool growstill=1;
	bool setorigin=0;
	int LoopLength = VesselListLength; 
	cout<<"Growth Begin"<<endl;
	VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
	VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
	VesselCell* pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
	VesselCell* pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
	VesselCell* pTempVesselCell = TheVessel->pFirstVesselCell;
	
	while(growstill || !setorigin)
	{
		
		cout<<"Once Begin"<<endl;
		growstill = 0;
		
		
		Prob0 = zeros(Xnodes, Ynodes);
		Prob1 = zeros(Xnodes, Ynodes);
		Prob2 = zeros(Xnodes, Ynodes);
		Prob3 = zeros(Xnodes, Ynodes);
		Prob4 = zeros(Xnodes, Ynodes);
		Inf0 = zeros(Xnodes, Ynodes);
		Inf1 = zeros(Xnodes, Ynodes);
		Inf2 = zeros(Xnodes, Ynodes);
		Inf3 = zeros(Xnodes, Ynodes);
		Inf4 = zeros(Xnodes, Ynodes);
		Sup0 = zeros(Xnodes, Ynodes);
		Sup1 = zeros(Xnodes, Ynodes);
		Sup2 = zeros(Xnodes, Ynodes);
		Sup3 = zeros(Xnodes, Ynodes);
		Sup4 = zeros(Xnodes, Ynodes);
		
		
		pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
		pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
		pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
		pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
		pTempVesselCell = TheVessel->pFirstVesselCell;
		LoopLength = VesselListLength;
		VesselBroke=0;
		
		
		for(int Loop = 0; Loop < LoopLength; Loop++)
		{
			//cout<<"enter loop"<<endl;
			int i = pCurrentVesselPoint1->i;
			int j = pCurrentVesselPoint1->j;
			int i_origin = pCurrentVesselPoint1->i_origin;
			int j_origin = pCurrentVesselPoint1->j_origin;
			
			
			
			if((i==0||j==0||i==Xnodes-1||j==Ynodes-1)&&*SproutTips(i,j)==1)
			{
				*SproutTips(i,j)=0;
				//SproutNumber -=1;
			}
			
			
			if( *SproutTips(i,j) > 0)
			{
				
				
				//cout<<"enter sprouttip loop"<<endl;
				//cout<<"beginsprout";
				if (i == 0) {iminus=1;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
				if (j == 0) {jminus=1;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
				if (i == Xnodes-1) {iplus = Xnodes - 2;} else  {iplus = i+1;}
				if (j == Ynodes-1) {jplus = Ynodes - 2;} else  {jplus = j+1;}
				
				//Prob0 should be with stability 
				
				/*
				 *Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i, j) );
				 
				 *Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i, j) );
				 
				 *Prob3(i, j) = Chemo_Response*( *TAF(i, jplus) - *TAF(i, j) );	   
				 
				 *Prob4(i, j) = Chemo_Response*( *TAF(i, jminus) - *TAF(i, j) );
				 */
				*Prob0(i, j) = 0;
				*Prob1(i, j) = 0;
				*Prob2(i, j) = 0;
				*Prob3(i, j) = 0;
				*Prob4(i, j) = 0;
				if( *TAF(iplus, j) - *TAF(i, j) > 0.0)
				{
					*Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i_origin, j_origin) ) - String*pow((iplus-i_origin)*(iplus-i_origin)+(j-j_origin)*(j-j_origin), 0.5);
					// cout<< "*Prob1(i, j)"<<*Prob1(i, j)<<endl;
				}
				if(*TAF(iminus, j) - *TAF(i, j) > 0.0)
				{
					*Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i_origin, j_origin) ) - String*pow((iminus-i_origin)*(iminus-i_origin)+(j-j_origin)*(j-j_origin), 0.5);
					//	  cout<< "*Prob2(i, j)"<<*Prob2(i, j)<<endl;
				}
				if(*TAF(i, jplus) - *TAF(i, j) > 0.0)
				{
					//	cout<< "*Prob3(i, j)"<<*Prob3(i, j)<<endl;
					*Prob3(i, j) = Chemo_Response*( *TAF(i,jplus) - *TAF(i_origin, j_origin) )  - String*pow((i-i_origin)*(i-i_origin)+(jplus-j_origin)*(jplus-j_origin), 0.5);	   
				}
				if(*TAF(i, jminus) - *TAF(i, j) > 0.0)
				{
					
					//	cout<< "*Prob4(i, j)"<<*Prob4(i, j)<<endl;
					*Prob4(i, j) = Chemo_Response*(  *TAF(i,jminus) - *TAF(i_origin, j_origin) ) - String*pow((i-i_origin)*(i-i_origin)+(jminus-j_origin)*(jminus-j_origin), 0.5);
				}
				
				
				
				
				
				if( *Prob0(i,j) <= 0 )
				{
					*Prob0(i,j) = 0;
				}
				if( *Prob1(i,j) <= 0 )
				{
					*Prob1(i,j) = 0;
					
				}
				if( *Prob2(i,j) <= 0 )
				{
					*Prob2(i,j) = 0;
					
				}
				if( *Prob3(i,j) <= 0 )
				{
					*Prob3(i,j) = 0;
					
				}
				
				if( *Prob4(i,j) <= 0 )
				{
					*Prob4(i,j) = 0;
					
				}
				
				
				
				
				
				//the sprout shouldn't grow reverse  
				if (*BloodVesselIndicatorX(i,j) < 0 )
				{
					*Prob2(i,j) = 0;
				}
				else if (*BloodVesselIndicatorX(i,j) > 0)
				{
					*Prob1(i,j) = 0;
				}
				else if (*BloodVesselIndicatorY(i,j) < 0 )
				{
					*Prob4(i,j) = 0;
				}
				else if (*BloodVesselIndicatorY(i,j) > 0 )
				{
					*Prob3(i,j) = 0;
				}
				//other exceptions 
				if (i == Xnodes-1) 
				{ *Prob1(i, j) = 0.0; }
				else if (i == 0)   
				{ *Prob2(i, j) = 0.0; }
				if (j == ( Ynodes-1))
				{ *Prob3(i, j) = 0.0; }
				if (j == 0)
				{ *Prob4(i, j) = 0.0; }
				
				
				// cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
				//rescale the probabilities
				double ProbTotal = *Prob0(i,j) + *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j) ;
				if ( ProbTotal> 0 )
				{
					growstill = 1;
					
					*Prob0(i,j) /= ProbTotal;
					*Prob1(i,j) /= ProbTotal;
					*Prob2(i,j) /= ProbTotal;
					*Prob3(i,j) /= ProbTotal;
					*Prob4(i,j) /= ProbTotal;
					
					cout<<*Prob0(i,j)<<"  "<<*Prob1(i,j)<<"  " <<*Prob2(i,j)<<"  "<<*Prob3(i,j)<<"  " <<*Prob4(i,j)<<endl;
					
					//Get Random Intervals
					*Inf0(i,j) = 0;
					*Sup0(i,j) = *Inf0(i,j) + *Prob0(i,j);
					//*Sup0(i,j) = *Inf0(i,j) + *Prob1(i,j);
					*Inf1(i,j) = *Sup0(i,j);
					*Sup1(i,j) = *Inf1(i,j) + *Prob1(i,j);
					//*Sup1(i,j) = *Inf1(i,j) + *Prob0(i,j);
					*Inf2(i,j) = *Sup1(i,j);
					*Sup2(i,j) = *Inf2(i,j) + *Prob2(i,j);
					*Inf3(i,j) = *Sup2(i,j);
					*Sup3(i,j) = *Inf3(i,j) + *Prob3(i,j);
					//*Sup3(i,j) = *Inf3(i,j) + *Prob4(i,j);
					*Inf4(i,j) = *Sup3(i,j);   //not neccesary, delete them to speed up the program
					*Sup4(i,j) = *Inf4(i,j) + *Prob4(i,j);
					//*Sup4(i,j) = *Inf4(i,j) + *Prob3(i,j);
					
					//Done with those probabilities, begin to Move and Connect in one step.
					double RandNumber;
					int I, J;
					RandNumber = float( rand() ) / float( RAND_MAX);
					
					if(RandNumber >=*Inf0(i,j) && RandNumber < *Sup0(i,j))
					{
						
					}
					
					else if(RandNumber >=*Inf1(i,j) && RandNumber < *Sup1(i,j))
					{
						I = i + 1;
						J = j;
						
						
						if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
								SproutNumber -= 1;//comment this we have anasmosis
								
							}
							
							
							
							//*DirectionX(I, J) = 1;
							//*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
							//SproutNumber -=1;//Add this we have anasmosis
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;
							*SproutTips(i, j) = 0;	
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
							
							
							
							
						}
						else
						{
							
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionX(I, J) = 1;
							
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
							
							
							
							
						}
						
						
						
						if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
						{
							*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorX(i,j)<-0.5)
							{*BloodVesselIndicatorX(i,j)=2;}
							else
							{*BloodVesselIndicatorX(i,j)=1;}
						}
						if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
						{
							*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorX(I,J)>0.5)
							{*BloodVesselIndicatorX(I,J)=2;}
							else
							{*BloodVesselIndicatorX(I,J)=-1;}
						}
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
						
						
					}
					
					else if(RandNumber >= *Inf2(i,j) && RandNumber < *Sup2(i,j))
					{
						I = i - 1;
						J = j;
						
						
						if(  (min_abs(*BloodVesselIndicator(I, J))) > 0 )
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; 
								SproutNumber -= 1;	
								
								
							}
							
							
							// *DirectionX(I, J) = -1;
							// *SproutTips(I, J) = 1;
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;	
							//SproutNumber -= 1;
							*SproutTips(i, j) = 0;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
						}
						else
						{
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob2 make Vessel Length"<<VesselListLength<<endl;
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionX(I, J) = -1;
							
							
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
							
							
						}
						
						
						if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
						{
							*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorX(i,j)>0.5)
							{*BloodVesselIndicatorX(i,j)=2;}
							else
							{*BloodVesselIndicatorX(i,j)=-1;}
						}
						if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
						{
							*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorX(I,J)<-0.5)
							{*BloodVesselIndicatorX(I,J)=2;}
							else
							{*BloodVesselIndicatorX(I,J)= 1;}
						}
						
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
					}
					
					else if(RandNumber >=*Inf3(i,j) && RandNumber < *Sup3(i,j))
					{
						I = i;
						J = j + 1;
						
						if( (*BloodVesselIndicator(I, J))> 0)
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; 
								SproutNumber -= 1;
								
								
							}
							
							
							
							// SproutNumber -= 1;
							// *DirectionY(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;	 
							*SproutTips(i, j) = 0;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
						}
						else
						{
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob3 make Vessel Length"<<VesselListLength<<endl;
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionY(I, J) = 1;
							
							
							//cout<<"here"<<endl;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							//cout<<pCurrentVesselCell1->cIndex<<endl;
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//cout<<pCurrentVesselCell2->cIndex<<endl;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
								
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
						}
						
						if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
						{
							*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorY(i,j)<-0.5)
							{*BloodVesselIndicatorY(i,j)=2;}
							else
							{*BloodVesselIndicatorY(i,j)=1;}
						}
						if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
						{
							*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorY(I,J)>0.5)
							{*BloodVesselIndicatorY(I,J)=2;}
							else
							{*BloodVesselIndicatorY(I,J)=-1;}
						}
						
						
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
					}
					
					else if(RandNumber >= *Inf4(i,j) && RandNumber < *Sup4(i,j))
					{
						I = i;
						J = j - 1;
						
						
						if( (*BloodVesselIndicator(I, J)) > 0)
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; 
								SproutNumber -= 1;
							}
							
							
							// *DirectionY(I, J) = -1;
							// *SproutTips(I, J) = 1;
							//SproutNumber -= 1;
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;	
							*SproutTips(i, j) = 0;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
						}
						else
						{		
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob4 make Vessel Length"<<VesselListLength<<endl;
							
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionY(I, J) = -1;
							
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
							
						}
						
						if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
						{
							*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorY(i,j)>0.5)
							{*BloodVesselIndicatorY(i,j)=2;}
							else
							{*BloodVesselIndicatorY(i,j)=-1;}
						}
						if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
						{
							*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorY(I,J)<-0.5)
							{*BloodVesselIndicatorY(I,J)=2;}
							else
							{*BloodVesselIndicatorY(I,J)=1;}
						}
						
						
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
					}
					
					//BOUNDARY SPROUTS We can ask it to be periodical?
				}//probtotal>0
				else
				{
					//cout<<"Show if i come here"<<endl;
					//getchar();
					*SproutTips(i,j) = 0;
				}//probtotal=0
				
			}//this is for the if sprout
			
			
			pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
			//cout<<"aftermove";
		}//this is for the loop
		pLastVesselPoint = pCurrentVesselPoint2;
		TheVessel->pLastVesselCell = pCurrentVesselCell2;
		//QuickPlot();// COMBINE
		cout <<"GrowSprout once"<<endl; 
		if(!growstill)
		{
			
			pCurrentVesselPoint1 = pFirstVesselPoint;
			
			
			
			while(pCurrentVesselPoint1)
			{
				pCurrentVesselPoint1->i_origin = pCurrentVesselPoint1->i;
				pCurrentVesselPoint1->j_origin = pCurrentVesselPoint1->j;
				
				
				
				pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
			}
			
			
			
			
			setorigin = 1;
			
			
			
			
			cout <<"origin reset"<<endl; 
			
		}//setorigin 
		
		
		
	}//still loop
	cout <<"function GrowSprout finish"<<endl;
}


void VesselStructure::GrowSprout()
{
//cout <<"function GrowSprout running"<<endl; 

extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern Matrix TAF;
extern Matrix ECM;
extern int Xnodes, Ynodes;
double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
//sweep the list to update rather than matrix?
//yes!
//should we have more list?
//we don't need to, because even if we have two main vessel,
//it's like a whole system with the general BC and IC.

//DON'T FORGET THE INDICATOR
AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
AngioDeltaY = 1.0 /Ynodes;
Matrix Prob0 = zeros(Xnodes, Ynodes);
Matrix Prob1 = zeros(Xnodes, Ynodes);
Matrix Prob2 = zeros(Xnodes, Ynodes);
Matrix Prob3 = zeros(Xnodes, Ynodes);
Matrix Prob4 = zeros(Xnodes, Ynodes);
Matrix Inf0 = zeros(Xnodes, Ynodes);
Matrix Inf1 = zeros(Xnodes, Ynodes);
Matrix Inf2 = zeros(Xnodes, Ynodes);
Matrix Inf3 = zeros(Xnodes, Ynodes);
Matrix Inf4 = zeros(Xnodes, Ynodes);
Matrix Sup0 = zeros(Xnodes, Ynodes);
Matrix Sup1 = zeros(Xnodes, Ynodes);
Matrix Sup2 = zeros(Xnodes, Ynodes);
Matrix Sup3 = zeros(Xnodes, Ynodes);
Matrix Sup4 = zeros(Xnodes, Ynodes);
int iminus,iplus,jminus,jplus;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
int LoopLength = VesselListLength; 
for(int Loop = 0; Loop < LoopLength; Loop++)
{
   //cout<<"enter loop"<<endl;
	int i = pCurrentVesselPoint1->i;
	int j = pCurrentVesselPoint1->j;
 // cout<<i<<"    "<<j<<endl;
 
    
	if(i==1&&*SproutTips(i,j)==1)
	{
	*SproutTips(i,j)=0;
	SproutNumber -=1;
	}
	else if(i==Xnodes-2&&*SproutTips(i,j)==1)
	{
	*SproutTips(i,j)=0;
	SproutNumber -=1;
	}
	if( *SproutTips(i,j) > 0)
	{
	//cout<<"enter sprouttip loop"<<endl;
	    if (i == 0) {iminus=1;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=1;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = Xnodes - 2;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = Ynodes - 2;} else  {jplus = j+1;}
		
	       /****  			 
		*Prob0(i, j) = 1.0
	                 - 2.0 * Dif_ECM * AngioDeltaT * ( (1.0/pow(AngioDeltaX,2) ) + (1.0/pow(AngioDeltaY,2) ) )
					 + ( ( Chemo_Response * Chemo_Sense * AngioDeltaT ) / pow( ( 1.0 + Chemo_Sense * *TAF(i,j) ), 2) ) 
						* ( (pow( ( *TAF(iplus, j) - *TAF(iminus, j) ), 2) / pow( (2.0*AngioDeltaX), 2) )
	                       +(pow( ( *TAF(i, jplus) - *TAF(i, jminus) ), 2) / pow( (2.0*AngioDeltaY), 2) ) ) 
                     - ( (Chemo_Response * AngioDeltaT) / ( 1.0 + Chemo_Sense* *TAF(i, j) ) )
	                    *( ( ( *TAF(iplus, j) - 2.0 * *TAF(i, j) + *TAF(iminus, j) ) / pow(AngioDeltaX, 2) )
                          +( ( *TAF(i, jplus) - 2.0 * *TAF(i, j) + *TAF(i, jminus) ) / pow(AngioDeltaY, 2) ) )
	                 - Haptotactic_Reponse * AngioDeltaT * ( ( ( *ECM(iplus, j) - 2.0 * *ECM(i, j) + *ECM(iminus, j) ) / pow(AngioDeltaX, 2) )
	                                         +( ( *ECM(i, jplus) - 2.0 * *ECM(i, j) + *ECM(i, jminus) ) / pow(AngioDeltaY, 2) ) );
											 
	  
		  *Prob1(i, j) = ( Dif_ECM * AngioDeltaT ) / pow( AngioDeltaX, 2 )
						+ ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(iplus, j) - *TAF(i, j) )
		                      + Haptotactic_Reponse * ( *ECM(iplus, j) - *ECM(i, j) ) );
		
		


	 
	
	     *Prob2(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaX, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, j) - *TAF(iminus, j) )
		                      + Haptotactic_Reponse * ( *ECM(i, j) - *ECM(iminus, j) ) );
	
	
	
	     *Prob3(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       + ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, jplus) - *TAF(i, j) )
		                      + Haptotactic_Reponse * ( *ECM(i, jplus) - *ECM(i, j) ) );
	
	   
    
	     *Prob4(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, j) - *TAF(i, jminus) )
		                      + Haptotactic_Reponse * ( *ECM(i, j) - *ECM(i, jminus) ) );
		  *********/
		 
		*Prob0(i, j) = 1.0
	                 - 2.0 * Dif_ECM * AngioDeltaT * ( (1.0/pow(AngioDeltaX,2) ) + (1.0/pow(AngioDeltaY,2) ) )
					 + ( ( Chemo_Response * Chemo_Sense * AngioDeltaT ) / pow( ( 1.0 + Chemo_Sense * *TAF(i,j) ), 2) ) 
						* ( (pow( ( *TAF(iplus, j) - *TAF(iminus, j) ), 2) / pow( (2.0*AngioDeltaX), 2) )
	                       +(pow( ( *TAF(i, jplus) - *TAF(i, jminus) ), 2) / pow( (2.0*AngioDeltaY), 2) ) ) 
                     - ( (Chemo_Response * AngioDeltaT) / ( 1.0 + Chemo_Sense* *TAF(i, j) ) )
	                    *( ( ( *TAF(iplus, j) - 2.0 * *TAF(i, j) + *TAF(iminus, j) ) / pow(AngioDeltaX, 2) )
                          +( ( *TAF(i, jplus) - 2.0 * *TAF(i, j) + *TAF(i, jminus) ) / pow(AngioDeltaY, 2) ) )
	                 - Haptotactic_Reponse * AngioDeltaT * ( ( ( *ECM(iplus, j) - 2.0 * *ECM(i, j) + *ECM(iminus, j) ) / pow(AngioDeltaX, 2) )
	                                         +( ( *ECM(i, jplus) - 2.0 * *ECM(i, j) + *ECM(i, jminus) ) / pow(AngioDeltaY, 2) ) );
											 
	  
		  *Prob1(i, j) = ( Dif_ECM * AngioDeltaT ) / pow( AngioDeltaX, 2 )
						+ ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(iplus, j) - *TAF(iminus, j) )
		                      + Haptotactic_Reponse * ( *ECM(iplus, j) - *ECM(iminus, j) ) );
		
		


	 
	
	     *Prob2(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaX, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(iplus, j) - *TAF(iminus, j) )
		                      + Haptotactic_Reponse * ( *ECM(iplus, j) - *ECM(iminus, j) ) );
	
	
	
	     *Prob3(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       + ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, jplus) - *TAF(i, jminus) )
		                      + Haptotactic_Reponse * ( *ECM(i, jplus) - *ECM(i, jminus) ) );
	
	   
    
	     *Prob4(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, jplus) - *TAF(i, jminus) )
		                      + Haptotactic_Reponse * ( *ECM(i, jplus) - *ECM(i, jminus) ) );
							  
							

   //cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
	 // make probabilities above 0  
	   double MakeUpToZero1, MakeUpToZero2, MakeUpToZero3, MakeUpToZero4;
	  
			MakeUpToZero1 = 0;
			MakeUpToZero2 = 0;
			MakeUpToZero3 = 0;
			MakeUpToZero4 = 0;
			
		if( *Prob0(i,j) < 0 )
		{
		*Prob0(i,j) = 0;
		}
		if( *Prob1(i,j) < 0 )
		{
		MakeUpToZero1 = min_abs(*Prob1(i,j));
	
		}
		if( *Prob2(i,j) < 0 )
		{
		MakeUpToZero2 = min_abs(*Prob2(i,j));
		
		}
		if( *Prob3(i,j) < 0 )
		{
		MakeUpToZero3 = min_abs(*Prob3(i,j));
		
		}
		if( *Prob4(i,j) < 0 )
		{
		MakeUpToZero4 = min_abs(*Prob4(i,j));
		
		}
       
	   *Prob1(i,j) += (MakeUpToZero1 + MakeUpToZero2);
	   *Prob2(i,j) += (MakeUpToZero1 + MakeUpToZero2);
	   *Prob3(i,j) += (MakeUpToZero3 + MakeUpToZero4);
	   *Prob4(i,j) += (MakeUpToZero3 + MakeUpToZero4);
	   
	   
	 //the sprout shouldn't grow reverse  
	   if (*BloodVesselIndicatorX(i,j) < 0 )
	   {
	   *Prob2(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorX(i,j) > 0)
	   {
	   *Prob1(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) < 0 )
	   {
	   *Prob4(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) > 0 )
	   {
	   *Prob3(i,j) = 0;
	   }
	   //other exceptions 
		if (i == Xnodes-1) 
	    { *Prob1(i, j) = 0.0; }
		else if (i == 0)   
	    { *Prob2(i, j) = 0.0; }
		if (j >= ( Ynodes-25))
	    { *Prob3(i, j) = 0.0; }
		 if (j == 0)
	    { *Prob4(i, j) = 0.0; }


   // cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
     //rescale the probabilities
	 double ProbTotal = *Prob0(i,j) + *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j) ;
	 if ( ProbTotal> 0 )
	 {
		*Prob0(i,j) /= ProbTotal;
		*Prob1(i,j) /= ProbTotal;
		*Prob2(i,j) /= ProbTotal;
		*Prob3(i,j) /= ProbTotal;
		*Prob4(i,j) /= ProbTotal;
	 }
	// cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
	 
	 //Get Random Intervals
	 *Inf0(i,j) = 0;
	 *Sup0(i,j) = *Inf0(i,j) + *Prob0(i,j);
	 //*Sup0(i,j) = *Inf0(i,j) + *Prob1(i,j);
	 *Inf1(i,j) = *Sup0(i,j);
	 *Sup1(i,j) = *Inf1(i,j) + *Prob1(i,j);
	 //*Sup1(i,j) = *Inf1(i,j) + *Prob0(i,j);
	 *Inf2(i,j) = *Sup1(i,j);
	 *Sup2(i,j) = *Inf2(i,j) + *Prob2(i,j);
	 *Inf3(i,j) = *Sup2(i,j);
	 *Sup3(i,j) = *Inf3(i,j) + *Prob3(i,j);
	 //*Sup3(i,j) = *Inf3(i,j) + *Prob4(i,j);
	 *Inf4(i,j) = *Sup3(i,j);   //not neccesary, delete them to speed up the program
	 *Sup4(i,j) = *Inf4(i,j) + *Prob4(i,j);
	 //*Sup4(i,j) = *Inf4(i,j) + *Prob3(i,j);
	 
	 //Done with those probabilities, begin to Move and Connect in one step.
	 
	 double RandNumber;
	 int I, J;
	 RandNumber = float( rand() ) / float( RAND_MAX);
	 if(RandNumber >=*Inf0(i,j) && RandNumber < *Sup0(i,j))
	 {
		I = i;
		J = j;
		*SproutAge(I, J) += AngioDeltaT;
	 }
	 
	 else if(RandNumber >=*Inf1(i,j) && RandNumber < *Sup1(i,j))
	 {
		I = i + 1;
		J = j;
		
		if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
	     if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
			SproutNumber -= 1;//comment this we have anasmosis
			
		  }
		  
		  		  
		 *SproutTips(i, j) = 0;
		 //*DirectionX(I, J) = 1;
		 *SproutTips(I, J) = 0;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	 
		}
		else
		{
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionX(I, J) = 1;
		}
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }
     
	 else if(RandNumber >= *Inf2(i,j) && RandNumber < *Sup2(i,j))
	 {
		I = i - 1;
		J = j;
		
		if(  (min_abs(*BloodVesselIndicator(I, J))) > 0 )
		{
		
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
			SproutNumber -= 1;	
			
	
		  }
		 
		  		  
		 *SproutTips(i, j) = 0;
		// *DirectionX(I, J) = -1;
		 *SproutTips(I, J) = 0;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	
		 //SproutNumber -= 1;
	 
		}
		else
		{
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob2 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionX(I, J) = -1;
		}
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	 }
	 
	 else if(RandNumber >=*Inf3(i,j) && RandNumber < *Sup3(i,j))
	 {
		I = i;
		J = j + 1;
		
		
		if( (*BloodVesselIndicator(I, J))> 0)
		{
		 
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
				SproutNumber -= 1;
				  

		  }
		 
			  
		 *SproutTips(i, j) = 0;
		// SproutNumber -= 1;
		// *DirectionY(I, J) = 1;
		 *SproutTips(I, J) = 0;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	 
		}
		else
		{
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob3 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 0;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionY(I, J) = 1;
		}
		
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	 }
	 
	 else if(RandNumber >= *Inf4(i,j) && RandNumber < *Sup4(i,j))
	 {
		I = i;
		J = j - 1;
				
		if( (*BloodVesselIndicator(I, J)) > 0)
		{
		 
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
			 SproutNumber -= 1;
		  }
		 		  
		 *SproutTips(i, j) = 0;
		// *DirectionY(I, J) = -1;
		 *SproutTips(I, J) = 1;
		 //SproutNumber -= 1;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	
		}
		else
		{		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob4 make Vessel Length"<<VesselListLength<<endl;

		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionY(I, J) = -1;
		}
		
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	 }

	//BOUNDARY SPROUTS We can ask it to be periodical?
	
	 if(I == 0 || I == Xnodes - 1 || J == 0 || J == Ynodes - 1)
	 {
	  *SproutTips(I, J) = 0;
	  *SproutAge(I, J) = 0;
	 }
	 
	  // cout<<"quit sprouttip loop"<<endl;

	 
	 }//this is for the if
	 else
	 {
		*VesselAge(i,j) += AngioDeltaT; 
		//cout<<"Age"<<*VesselAge(i,j);
	 }
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
    }//this is for the loop
   pLastVesselPoint = pCurrentVesselPoint2;
   QuickPlot();// COMBINE
   //cout <<"function GrowSprout finished"<<endl; 
 }
 


void VesselStructure::VesselAdaption()
{

double Pin, Pout;
int Iteration = 50000;
double Tolerance = 0.001;//0.0001;If you change it, need change ptupdate as well

extern Matrix Permeability;
extern Matrix VesselAge;

extern Matrix WallGradient;
extern Matrix BloodNodePressure;
extern Matrix HaematocritLevel;
extern int Xnodes, Ynodes;
double MaxConduct;
 Permeability=ones(Xnodes, Ynodes);
 WallGradient=zeros(Xnodes, Ynodes);
UpdatePermeability();
	extern Matrix TracerS;

extern Matrix X, Y;
extern double TotalIn;
extern Matrix LymIndicator;
Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
Pout = RetrieveDoubleParameter( ParameterFile , "Pout");
extern Matrix ConductX, ConductY;
extern Matrix TracerS;

extern int Xnodes, Ynodes;
cout <<"function VesselAdaption running"<<endl; 
extern bool AdaptionBegin;
extern bool ResetPressure;
extern double t;
extern char SaveDirectory[1025];
 char temp_string[1024];
//getchar();


UpdateConductivity();


if(ResetPressure)
 {
 BloodNodePressure = zeros(Xnodes, Ynodes);
 HaematocritLevel = zeros(Xnodes, Ynodes);
 //InterAngioPressureUpdate(Interation, Tolerance);
 	 
 }
//InterAngioPressureUpdate(Iteration, Tolerance);
	UpdateOxyVWTPressure();	
//GetVesselPressureAndFlow(Tolerance);
//SteadyStateHematocrit();	
//LeakAngioPressureUpdate(Iteration, Tolerance);


	

	
	UpdateViscosity();
UpdateWallShearStress();


//if(t>8&&t<8.1||t>10&&t<10.1||t>12&&t<12.1)
 
  //if(t>0.095&&t<0.145)
  //if(t>=8.00)
    //{
    //OutPutStudy();
	//Transportation_P();
//	Transportation_DI(t);
	//GetMaxTracer();
  //  }

   
	 if(t>6&&t<6.1||t>8&&t<8.1)
  
  {
  /****These two following lines are used for parameter study only***/
   // OutPutStudy();
	 // TracerS = zeros(Xnodes, Ynodes);
	  
	  
	//Transportation_P();
	//Transportation_D();
	//GetMaxTracer();
}

UpdateRadius();

//Adhere();


//for(int i=0;i<10;i++)
//{







//UpdateConductivity();


//}


   

cout <<"function VesselAdaption finished"<<endl; 


}


void VesselStructure::OutPutStudy()
{
 double PI = 3.141592654;
extern double TotalIn;
extern char SaveDirectory[1025];
 char temp_string[1024];
 extern Matrix Phi;
 extern int Xnodes, Ynodes;
 extern Matrix BloodVesselIndicator,Flow,BloodVesselRadius;
	extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
	extern Matrix BloodVesselRadiusXdir, BloodVesselRadiusYdir;
 extern Matrix TracerS;
 extern Matrix PreExist;
 extern double t;
 extern Matrix SrtX, SrtY;
	
	Matrix Paolo, PaoloS;
	Paolo = zeros(Xnodes, Ynodes);
	PaoloS = zeros(Xnodes, Ynodes);
	
//FIRST SUM, THEN AVERAGE
double FANs = 0, FAOs = 0, FTNs= 0,FTOs= 0, FANa=0,FAOa=0, FTNa=0,FTOa=0, RAO=0,RAN=0, RTN=0,RTO=0, BTN=0,BTO=0, BAN=0,BAO=0, PT = 0, PA = 0;
sprintf( temp_string, "%s/FT_%d_%f.txt", SaveDirectory,1,t);
     ofstream FLOWT( temp_string, ios::out );
	 
sprintf( temp_string, "%s/FA_%d_%f.txt", SaveDirectory,1,t);
     ofstream FLOWA( temp_string, ios::out );

sprintf( temp_string, "%s/RT_%d_%f.txt", SaveDirectory,1,t);
     ofstream RADIUST( temp_string, ios::out );
	 
sprintf( temp_string, "%s/RA_%d_%f.txt", SaveDirectory,1,t);
     ofstream RADIUSA( temp_string, ios::out );
for( int i=0;i<Xnodes;i++)
 {
	for( int j=0;j<Ynodes;j++)
	{
		
		if(*Phi(i,j)<0)
		 {
			PT += 1;
		 }
		 else
		 {
		    PA += 1;
		 }
		
		if(*BloodVesselIndicator(i,j)>0)
		{
		 if(*Phi(i,j)<0)
		 {		 
		 if(*PreExist(i,j)==0)
		 {
		 FTNs += *Flow(i,j);
		 RTN  += *BloodVesselRadius(i,j);		 
		 BTN += 1;
		 }
		 else
		 {
		 FTOs += *Flow(i,j);
		 RTO  += *BloodVesselRadius(i,j);		 
		 BTO += 1;
		 }
		 }
		 else
		 {
		 if(*PreExist(i,j)==0)
		 {
		 FANs += *Flow(i,j);
		 RAN  += *BloodVesselRadius(i,j);
		 BAN += 1;
		 }
		 else
		 {
		 FAOs += *Flow(i,j);
		 RAO  += *BloodVesselRadius(i,j);
		 BAO += 1;
		 }
		
		 }
			
	    }
		 		 
	}
 }
	
	
 PA = PA/10000;
 PT = PT/10000;

 FTNa = FTNs/BTN;
 FANa = FANs/BAN;
 FTOa = FTOs/BTO;
 FAOa = FAOs/BAO;
 RTN = RTN/BTN;
 RAN = RAN/BAN;
 RTO = RTO/BTO;
 RAO = RAO/BAO;		 
FLOWT<<FTNs<<"\t";
FLOWT<<FTOs<<"\t";
FLOWT<<FTNa<<"\t";
FLOWT<<FTOa<<"\t";
//FLOWA<<TotalIn<<"\t";
FLOWA<<FANs<<"\t";
FLOWA<<FAOs<<"\t";
FLOWA<<FANa<<"\t";
FLOWA<<FAOa<<"\t";

RADIUST<<RTN<<"\t";
RADIUST<<RTO<<"\t";
RADIUST<<BTN<<"\t";
RADIUST<<BTO<<"\t";
RADIUST<<PT<<"\t";
RADIUSA<<RAN<<"\t";
RADIUSA<<RAO<<"\t";
RADIUSA<<BAN<<"\t";
RADIUSA<<BAO<<"\t";
RADIUSA<<PA<<"\t";
FLOWT.close();
FLOWA.close();
RADIUST.close();
RADIUSA.close();

	/**Get the total surface area of new/old vessel in tumor/tissue area**/
	
	
	double SurfaceNT = 0, SurfaceOT = 0, SurfaceNA = 0, SurfaceOA = 0;
	double SrtNT = 0, SrtOT = 0, SrtNA = 0, SrtOA = 0;
	int countNT = 0,countOT= 0,countNA = 0, countOA = 0;
	//sprintf( temp_string, "%s/AVSRT_%d_%f.txt", SaveDirectory,1,t);
	//ofstream AVSRT( temp_string, ios::out );
	sprintf( temp_string, "%s/SURFACE_%d_%f.txt", SaveDirectory,1,t);
	ofstream SURFACE( temp_string, ios::out );

	
	for( int i=0;i<Xnodes;i++)
	{
		for( int j=0;j<Ynodes;j++)
		{
			
						
			if(*BloodVesselIndicator(i,j)>0)
			{
				
				
				
				if(*Phi(i,j)<0)
				{		 
					if(*PreExist(i,j)==0)
					{
						if(j!=0&&(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5))
						{
							SurfaceNT += 1.0e6*2*PI* *BloodVesselRadiusXdir(i, j)*NodeLength;
							SrtNT += *SrtX(i,j);
							countNT +=1;
						}
						if(j!=0&&(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5))
						{
							SurfaceNT += 1.0e6*2*PI* *BloodVesselRadiusYdir(i, j)*NodeLength;
							SrtNT += *SrtY(i,j);
							countNT +=1;
						}
					}
					else
					{
						if(j!=0&&(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5))
						{
							SurfaceOT += 1.0e6*2*PI* *BloodVesselRadiusXdir(i, j)*NodeLength;
							SrtOT += *SrtX(i,j);
							countOT +=1;
						}
						if(j!=0&&(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5))
						{
							SurfaceOT += 1.0e6*2*PI* *BloodVesselRadiusYdir(i, j)*NodeLength;
							SrtOT += *SrtY(i,j);
							countOT +=1;
						}
						
					}
				}
				else
				{
					if(*PreExist(i,j)==0)
					{
						if(j!=0&&(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5))
						{
							SurfaceNA += 1.0e6*2*PI* *BloodVesselRadiusXdir(i, j)*NodeLength;
							SrtNA += *SrtX(i,j);
							countNA +=1;

						}
						if(j!=0&&(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5))
						{
							SurfaceNA += 1.0e6*2*PI* *BloodVesselRadiusYdir(i, j)*NodeLength;
							SrtNA += *SrtY(i,j);
							countNA +=1;
						}
					}
					else
					{
						if(j!=0&&(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5))
						{
							SurfaceOA += 1.0e6*2*PI* *BloodVesselRadiusXdir(i, j)*NodeLength;
							SrtOA += *SrtX(i,j);
							countOA +=1;
						}
						if(j!=0&&(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5))
						{
							SurfaceOA += 1.0e6*2*PI* *BloodVesselRadiusYdir(i, j)*NodeLength;
							SrtOA += *SrtY(i,j);
							countOA +=1;
						}				
					}
					
				}
				
			}
			
		}
	}
	
	
	SrtNT = SrtNT/countNT;
	SrtNA = SrtNA/countNA;
	SrtOT = SrtOT/countOT;
	SrtOA = SrtOA/countOA;
	
double DSrtNT = 0, DSrtNA = 0, DSrtOT = 0, DSrtOA = 0;
	
	
	for( int i=0;i<Xnodes;i++)
	{
		for( int j=0;j<Ynodes;j++)
		{
			
						
			if(*BloodVesselIndicator(i,j)>0)
			{
				
				
				
				if(*Phi(i,j)<0)
				{		 
					if(*PreExist(i,j)==0)
					{
						if(j!=0&&(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5))
						{
							
							DSrtNT += (*SrtX(i,j)-SrtNT)*(*SrtX(i,j)-SrtNT);
							
						}
						if(j!=0&&(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5))
						{
							
							DSrtNT += (*SrtY(i,j)-SrtNT)*(*SrtY(i,j)-SrtNT);
							
						}
					}
					else
					{
						if(j!=0&&(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5))
						{
							
							DSrtOT += (*SrtX(i,j)-SrtOT)*(*SrtX(i,j)-SrtOT);
							
						}
						if(j!=0&&(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5))
						{
							
							DSrtOT += (*SrtY(i,j)-SrtOT)*(*SrtY(i,j)-SrtOT);
							
						}
						
					}
				}
				else
				{
					if(*PreExist(i,j)==0)
					{
						if(j!=0&&(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5))
						{
							
							DSrtNA += (*SrtX(i,j)-SrtNA)*(*SrtX(i,j)-SrtNA);

							

						}
						if(j!=0&&(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5))
						{
							
							DSrtNA += (*SrtY(i,j)-SrtNA)*(*SrtY(i,j)-SrtNA);
							
						}
					}
					else
					{
						if(j!=0&&(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5))
						{
							
							DSrtOA += (*SrtX(i,j)-SrtOA)*(*SrtX(i,j)-SrtOA);
							
						}
						if(j!=0&&(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5))
						{
							
							DSrtOA += (*SrtY(i,j)-SrtOA)*(*SrtY(i,j)-SrtOA);
							
						}				
					}
					
				}
				
			}
			
		}
	}
	
	DSrtNT = DSrtNT/countNT;
	DSrtNA = DSrtNA/countNA;
	DSrtOT = DSrtOT/countOT;
	DSrtOA = DSrtOA/countOA;
	
	DSrtNT = pow(DSrtNT,0.5);
	DSrtNA = pow(DSrtNA,0.5);
	DSrtOT = pow(DSrtOT,0.5);
	DSrtOA = pow(DSrtOA,0.5);
	
	SurfaceNT = SurfaceNT/(1e-6 *25*PI);
	SurfaceNA = SurfaceNA/(1e-6 *25*PI);
	SurfaceOT = SurfaceOT/(1e-6 *25*PI);
	SurfaceOA = SurfaceOA/(1e-6 *25*PI);

		
	SURFACE<<SurfaceNT<<"\t";
	SURFACE<<SurfaceOT<<"\t";
	SURFACE<<SurfaceNA<<"\t";
	SURFACE<<SurfaceOA<<endl;
	
	SURFACE<<SrtNT<<"\t";
	SURFACE<<SrtOT<<"\t";
	SURFACE<<SrtNA<<"\t";
	SURFACE<<SrtOA<<endl;
	
	SURFACE<<DSrtNT<<"\t";
	SURFACE<<DSrtOT<<"\t";
	SURFACE<<DSrtNA<<"\t";
	SURFACE<<DSrtOA<<endl;
	
	double DensNT = countNT/PT, DensOT = countOT/PT, DensNA = countNA/PA,  DensOA = countOA/PA;
	
	SURFACE<<DensNT<<"\t";
	SURFACE<<DensOT<<"\t";
	SURFACE<<DensNA<<"\t";
	SURFACE<<DensOA<<endl;
	
	SURFACE.close();
	
	
	
double Pa, Pb, Pd;
double STN,STO, SAN,SAO;
double aSTN,aSTO, aSAN,aSAO;
double PTN,PTO, PAN,PAO;
double PSTN,PSTO, PSAN,PSAO;
double aPSTN,aPSTO, aPSAN,aPSAO;
Pd = 1.0e-7;

 sprintf( temp_string, "%s/STN_%d_%f.txt", SaveDirectory,1,t);
     ofstream ParticleSurfaceTN1( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/SAN_%d_%f.txt", SaveDirectory,1,t);
     ofstream ParticleSurfaceAN1( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/STO_%d_%f.txt", SaveDirectory,1,t);
     ofstream ParticleSurfaceTO1( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/SAO_%d_%f.txt", SaveDirectory,1,t);
     ofstream ParticleSurfaceAO1( temp_string, ios::out );
	
	
	sprintf( temp_string, "%s/aSTN_%d_%f.txt", SaveDirectory,1,t);
	ofstream aParticleSurfaceTN1( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aSAN_%d_%f.txt", SaveDirectory,1,t);
	ofstream aParticleSurfaceAN1( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aSTO_%d_%f.txt", SaveDirectory,1,t);
	ofstream aParticleSurfaceTO1( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aSAO_%d_%f.txt", SaveDirectory,1,t);
	ofstream aParticleSurfaceAO1( temp_string, ios::out );
	
	
 sprintf( temp_string, "%s/PTN_%d_%f.txt", SaveDirectory,1,t);
     ofstream PTN1( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PAN_%d_%f.txt", SaveDirectory,1,t);
     ofstream PAN1( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PTO_%d_%f.txt", SaveDirectory,1,t);
     ofstream PTO1( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PAO_%d_%f.txt", SaveDirectory,1,t);
     ofstream PAO1( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PSTN_%d_%f.txt", SaveDirectory,1,t);
     ofstream PSTN1( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PSAN_%d_%f.txt", SaveDirectory,1,t);
     ofstream PSAN1( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PSTO_%d_%f.txt", SaveDirectory,1,t);
     ofstream PSTO1( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PSAO_%d_%f.txt", SaveDirectory,1,t);
     ofstream PSAO1( temp_string, ios::out );
	
	
	sprintf( temp_string, "%s/aPSTN_%d_%f.txt", SaveDirectory,1,t);
	ofstream aPSTN1( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aPSAN_%d_%f.txt", SaveDirectory,1,t);
	ofstream aPSAN1( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aPSTO_%d_%f.txt", SaveDirectory,1,t);
	ofstream aPSTO1( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aPSAO_%d_%f.txt", SaveDirectory,1,t);
	ofstream aPSAO1( temp_string, ios::out );

	

Pa = 2.95e5;

for(int ii=0;ii<6;ii++)
{

Pb = 6.63e-5;
for(int jj=0;jj<3;jj++)
{
UpdateTracer_P(Pa, Pb, Pd, Paolo, PaoloS);
Transportation_P();
STN = 0.0;
SAN = 0.0;
STO = 0.0;
SAO = 0.0;
	aSTN = 0.0;
	aSAN = 0.0;
	aSTO = 0.0;
	aSAO = 0.0;
	
PTN = 0.0;
PAN = 0.0;
PTO = 0.0;
PAO = 0.0;
	PSTN = 0.0;
	PSAN = 0.0;
	PSTO = 0.0;
	PSAO = 0.0;
	
	aPSTN = 0.0;
	aPSAN = 0.0;
	aPSTO = 0.0;
	aPSAO = 0.0;
 for( int i=0;i<Xnodes;i++)
 {
	for( int j=0;j<Ynodes;j++)
	{
	
	if(*BloodVesselIndicator(i,j)>0)
	{
		 if(*Phi(i,j)<0&&*PreExist(i,j)==0)
		 {
		 STN += *TracerS(i,j);
		 PTN += *Paolo(i,j);
		 PSTN += *PaoloS(i,j);
		
		 }
		 else if(*Phi(i,j)<0&&*PreExist(i,j)>0)
		 {
		 STO += *TracerS(i,j);
		 PTO += *Paolo(i,j);
		 PSTO += *PaoloS(i,j);
		 }
		 else if(*Phi(i,j)>0&&*PreExist(i,j)==0)
		 {
		 SAN += *TracerS(i,j);
		 PAN += *Paolo(i,j);
		 PSAN += *PaoloS(i,j);
		
		 }
		 else if(*Phi(i,j)>0&&*PreExist(i,j)>0)
		 {
		 SAO += *TracerS(i,j);
		 PAO += *Paolo(i,j);
		 PSAO += *PaoloS(i,j);
		 }
	}
	
	}
	
	 
 }
 //aSTN = STN/SurfaceNT;
 //aSTO = STO/SurfaceOT;
 //aSAN = SAN/SurfaceNA;
 //aSAO = SAO/SurfaceOA;
 
 aSTN = STN/SurfaceNT;
 aSTO = STO/SurfaceOT;
 aSAN = SAN/SurfaceNA;
 aSAO = SAO/SurfaceOA;

 
    PTN = PTN/countNT;
	PAN = PAN/countNA;
	PTO = PTO/countOT;
	PAO = PAO/countOA;
 
 aPSTN = PSTN/SurfaceNT;
 aPSTO = PSTO/SurfaceOT;
 aPSAN = PSAN/SurfaceNA;
 aPSAO = PSAO/SurfaceOA;
 
 ParticleSurfaceTN1<<STN<<"\t";
 ParticleSurfaceAN1<<SAN<<"\t";
 ParticleSurfaceTO1<<STO<<"\t";
 ParticleSurfaceAO1<<SAO<<"\t";
	
	aParticleSurfaceTN1<<aSTN<<"\t";
	aParticleSurfaceAN1<<aSAN<<"\t";
	aParticleSurfaceTO1<<aSTO<<"\t";
	aParticleSurfaceAO1<<aSAO<<"\t";
	
	PTN1<<PTN<<"\t";
	PAN1<<PAN<<"\t";
	PTO1<<PTO<<"\t";
	PAO1<<PAO<<"\t";
	
	PSTN1<<PSTN<<"\t";
	PSAN1<<PSAN<<"\t";
	PSTO1<<PSTO<<"\t";
	PSAO1<<PSAO<<"\t";

	
	aPSTN1<<aPSTN<<"\t";
	aPSAN1<<aPSAN<<"\t";
	aPSTO1<<aPSTO<<"\t";
	aPSAO1<<aPSAO<<"\t";	

	
 Pb = 10*Pb;
}

 ParticleSurfaceTN1<<endl;
 ParticleSurfaceAN1<<endl;
 ParticleSurfaceTO1<<endl;
 ParticleSurfaceAO1<<endl;
	
	aParticleSurfaceTN1<<endl;
	aParticleSurfaceAN1<<endl;
	aParticleSurfaceTO1<<endl;
	aParticleSurfaceAO1<<endl;
	
	PTN1<<endl;
	PAN1<<endl;
	PTO1<<endl;
	PAO1<<endl;
	
	PSTN1<<endl;
	PSAN1<<endl;
	PSTO1<<endl;
	PSAO1<<endl;

	
	aPSTN1<<endl;
	aPSAN1<<endl;
	aPSTO1<<endl;
	aPSAO1<<endl;	
	
 Pa = 10*Pa;
}
 
ParticleSurfaceTN1.close();
ParticleSurfaceAN1.close();
ParticleSurfaceTO1.close();
ParticleSurfaceAO1.close();
	aParticleSurfaceTN1.close();
	aParticleSurfaceAN1.close();
	aParticleSurfaceTO1.close();
	aParticleSurfaceAO1.close();

PTN1.close();
PAN1.close();
PTO1.close();
PAO1.close();
PSTN1.close();
PSAN1.close();
PSTO1.close();
PSAO1.close();

aPSTN1.close();
aPSAN1.close();
aPSTO1.close();
aPSAO1.close();



Pd = 0.6e-6;
 sprintf( temp_string, "%s/STN_%d_%f.txt", SaveDirectory,2,t);
     ofstream ParticleSurfaceTN2( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/SAN_%d_%f.txt", SaveDirectory,2,t);
     ofstream ParticleSurfaceAN2( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/STO_%d_%f.txt", SaveDirectory,2,t);
     ofstream ParticleSurfaceTO2( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/SAO_%d_%f.txt", SaveDirectory,2,t);
     ofstream ParticleSurfaceAO2( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aSTN_%d_%f.txt", SaveDirectory,2,t);
	ofstream aParticleSurfaceTN2( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aSAN_%d_%f.txt", SaveDirectory,2,t);
	ofstream aParticleSurfaceAN2( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aSTO_%d_%f.txt", SaveDirectory,2,t);
	ofstream aParticleSurfaceTO2( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aSAO_%d_%f.txt", SaveDirectory,2,t);
	ofstream aParticleSurfaceAO2( temp_string, ios::out );
	
	
	 
	  
sprintf( temp_string, "%s/PTN_%d_%f.txt", SaveDirectory,2,t);
     ofstream PTN2( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PAN_%d_%f.txt", SaveDirectory,2,t);
     ofstream PAN2( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PTO_%d_%f.txt", SaveDirectory,2,t);
     ofstream PTO2( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PAO_%d_%f.txt", SaveDirectory,2,t);
     ofstream PAO2( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PSTN_%d_%f.txt", SaveDirectory,2,t);
     ofstream PSTN2( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PSAN_%d_%f.txt", SaveDirectory,2,t);
     ofstream PSAN2( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PSTO_%d_%f.txt", SaveDirectory,2,t);
     ofstream PSTO2( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PSAO_%d_%f.txt", SaveDirectory,2,t);
     ofstream PSAO2( temp_string, ios::out );
	
	
	sprintf( temp_string, "%s/aPSTN_%d_%f.txt", SaveDirectory,2,t);
	ofstream aPSTN2( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aPSAN_%d_%f.txt", SaveDirectory,2,t);
	ofstream aPSAN2( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aPSTO_%d_%f.txt", SaveDirectory,2,t);
	ofstream aPSTO2( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aPSAO_%d_%f.txt", SaveDirectory,2,t);
	ofstream aPSAO2( temp_string, ios::out );

	

Pa = 2.95e5;

for(int ii=0;ii<6;ii++)
{

Pb = 6.63e-5;
for(int jj=0;jj<3;jj++)
{
UpdateTracer_P(Pa, Pb, Pd, Paolo, PaoloS);
Transportation_P();
STN = 0.0;
SAN = 0.0;
STO = 0.0;
SAO = 0.0;
	aSTN = 0.0;
	aSAN = 0.0;
	aSTO = 0.0;
	aSAO = 0.0;
	
PTN = 0.0;
PAN = 0.0;
PTO = 0.0;
PAO = 0.0;
	PSTN = 0.0;
	PSAN = 0.0;
	PSTO = 0.0;
	PSAO = 0.0;
	
	aPSTN = 0.0;
	aPSAN = 0.0;
	aPSTO = 0.0;
	aPSAO = 0.0;
 for( int i=0;i<Xnodes;i++)
 {
	for( int j=0;j<Ynodes;j++)
	{
	
	if(*BloodVesselIndicator(i,j)>0)
	{
		 if(*Phi(i,j)<0&&*PreExist(i,j)==0)
		 {
		 STN += *TracerS(i,j);
		 PTN += *Paolo(i,j);
		 PSTN += *PaoloS(i,j);
		
		 }
		 else if(*Phi(i,j)<0&&*PreExist(i,j)>0)
		 {
		 STO += *TracerS(i,j);
		 PTO += *Paolo(i,j);
		 PSTO += *PaoloS(i,j);
		 }
		 else if(*Phi(i,j)>0&&*PreExist(i,j)==0)
		 {
		 SAN += *TracerS(i,j);
		 PAN += *Paolo(i,j);
		 PSAN += *PaoloS(i,j);
		
		 }
		 else if(*Phi(i,j)>0&&*PreExist(i,j)>0)
		 {
		 SAO += *TracerS(i,j);
		 PAO += *Paolo(i,j);
		 PSAO += *PaoloS(i,j);
		 }
	}
	
	}
	
	 
 }
	
	aSTN = STN/SurfaceNT;
	aSTO = STO/SurfaceOT;
	aSAN = SAN/SurfaceNA;
	aSAO = SAO/SurfaceOA;
	
 //aSTN = PT*STN/SurfaceNT;
 //aSTO = PT*STO/SurfaceOT;
 //aSAN = PA*SAN/SurfaceNA;
 //aSAO = PA*SAO/SurfaceOA;
	PTN = PTN/countNT;
	PAN = PAN/countNA;
	PTO = PTO/countOT;
	PAO = PAO/countOA;
 
 aPSTN = PSTN/SurfaceNT;
 aPSTO = PSTO/SurfaceOT;
 aPSAN = PSAN/SurfaceNA;
 aPSAO = PSAO/SurfaceOA;
	
	ParticleSurfaceTN2<<STN<<"\t";
	ParticleSurfaceAN2<<SAN<<"\t";
	ParticleSurfaceTO2<<STO<<"\t";
	ParticleSurfaceAO2<<SAO<<"\t";
	
	aParticleSurfaceTN2<<aSTN<<"\t";
	aParticleSurfaceAN2<<aSAN<<"\t";
	aParticleSurfaceTO2<<aSTO<<"\t";
	aParticleSurfaceAO2<<aSAO<<"\t";
	
	PTN2<<PTN<<"\t";
	PAN2<<PAN<<"\t";
	PTO2<<PTO<<"\t";
	PAO2<<PAO<<"\t";
	
	PSTN2<<PSTN<<"\t";
	PSAN2<<PSAN<<"\t";
	PSTO2<<PSTO<<"\t";
	PSAO2<<PSAO<<"\t";

	
	aPSTN2<<aPSTN<<"\t";
	aPSAN2<<aPSAN<<"\t";
	aPSTO2<<aPSTO<<"\t";
	aPSAO2<<aPSAO<<"\t";	
	


 Pb = 10*Pb;
}

 ParticleSurfaceTN2<<endl;
 ParticleSurfaceAN2<<endl;
 ParticleSurfaceTO2<<endl;
 ParticleSurfaceAO2<<endl;
	aParticleSurfaceTN2<<endl;
	aParticleSurfaceAN2<<endl;
	aParticleSurfaceTO2<<endl;
	aParticleSurfaceAO2<<endl;
	
	PTN2<<endl;
	PAN2<<endl;
	PTO2<<endl;
	PAO2<<endl;
	
	PSTN2<<endl;
	PSAN2<<endl;
	PSTO2<<endl;
	PSAO2<<endl;

	
	aPSTN2<<endl;
	aPSAN2<<endl;
	aPSTO2<<endl;
	aPSAO2<<endl;
 Pa = 10*Pa;
}
 
ParticleSurfaceTN2.close();
ParticleSurfaceAN2.close();
ParticleSurfaceTO2.close();
ParticleSurfaceAO2.close();
	aParticleSurfaceTN2.close();
	aParticleSurfaceAN2.close();
	aParticleSurfaceTO2.close();
	aParticleSurfaceAO2.close();

PTN2.close();
PAN2.close();
PTO2.close();
PAO2.close();
PSTN2.close();
PSAN2.close();
PSTO2.close();
PSAO2.close();

aPSTN2.close();
aPSAN2.close();
aPSTO2.close();
aPSAO2.close();


Pd = 1.0e-6;

 sprintf( temp_string, "%s/STN_%d_%f.txt", SaveDirectory,3,t);
     ofstream ParticleSurfaceTN3( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/SAN_%d_%f.txt", SaveDirectory,3,t);
     ofstream ParticleSurfaceAN3( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/STO_%d_%f.txt", SaveDirectory,3,t);
     ofstream ParticleSurfaceTO3( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/SAO_%d_%f.txt", SaveDirectory,3,t);
     ofstream ParticleSurfaceAO3( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aSTN_%d_%f.txt", SaveDirectory,3,t);
	ofstream aParticleSurfaceTN3( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aSAN_%d_%f.txt", SaveDirectory,3,t);
	ofstream aParticleSurfaceAN3( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aSTO_%d_%f.txt", SaveDirectory,3,t);
	ofstream aParticleSurfaceTO3( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aSAO_%d_%f.txt", SaveDirectory,3,t);
	ofstream aParticleSurfaceAO3( temp_string, ios::out );
	
	
	sprintf( temp_string, "%s/PTN_%d_%f.txt", SaveDirectory,3,t);
     ofstream PTN3( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PAN_%d_%f.txt", SaveDirectory,3,t);
     ofstream PAN3( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PTO_%d_%f.txt", SaveDirectory,3,t);
     ofstream PTO3( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PAO_%d_%f.txt", SaveDirectory,3,t);
     ofstream PAO3( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PSTN_%d_%f.txt", SaveDirectory,3,t);
     ofstream PSTN3( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PSAN_%d_%f.txt", SaveDirectory,3,t);
     ofstream PSAN3( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PSTO_%d_%f.txt", SaveDirectory,3,t);
     ofstream PSTO3( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/PSAO_%d_%f.txt", SaveDirectory,3,t);
     ofstream PSAO3( temp_string, ios::out );
	
	
	sprintf( temp_string, "%s/aPSTN_%d_%f.txt", SaveDirectory,3,t);
	ofstream aPSTN3( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aPSAN_%d_%f.txt", SaveDirectory,3,t);
	ofstream aPSAN3( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aPSTO_%d_%f.txt", SaveDirectory,3,t);
	ofstream aPSTO3( temp_string, ios::out );
	
	sprintf( temp_string, "%s/aPSAO_%d_%f.txt", SaveDirectory,3,t);
	ofstream aPSAO3( temp_string, ios::out );
Pa = 2.95e5;

for(int ii=0;ii<6;ii++)
{

Pb = 6.63e-5;
for(int jj=0;jj<3;jj++)
{
UpdateTracer_P(Pa, Pb, Pd, Paolo, PaoloS);
Transportation_P();
STN = 0.0;
SAN = 0.0;
STO = 0.0;
SAO = 0.0;
	aSTN = 0.0;
	aSAN = 0.0;
	aSTO = 0.0;
	aSAO = 0.0;
	
PTN = 0.0;
PAN = 0.0;
PTO = 0.0;
PAO = 0.0;
	PSTN = 0.0;
	PSAN = 0.0;
	PSTO = 0.0;
	PSAO = 0.0;
	
	aPSTN = 0.0;
	aPSAN = 0.0;
	aPSTO = 0.0;
	aPSAO = 0.0;
 for( int i=0;i<Xnodes;i++)
 {
	for( int j=0;j<Ynodes;j++)
	{
	
	if(*BloodVesselIndicator(i,j)>0)
	{
		 if(*Phi(i,j)<0&&*PreExist(i,j)==0)
		 {
		 STN += *TracerS(i,j);
		 PTN += *Paolo(i,j);
		 PSTN += *PaoloS(i,j);
		
		 }
		 else if(*Phi(i,j)<0&&*PreExist(i,j)>0)
		 {
		 STO += *TracerS(i,j);
		 PTO += *Paolo(i,j);
		 PSTO += *PaoloS(i,j);
		 }
		 else if(*Phi(i,j)>0&&*PreExist(i,j)==0)
		 {
		 SAN += *TracerS(i,j);
		 PAN += *Paolo(i,j);
		 PSAN += *PaoloS(i,j);
		
		 }
		 else if(*Phi(i,j)>0&&*PreExist(i,j)>0)
		 {
		 SAO += *TracerS(i,j);
		 PAO += *Paolo(i,j);
		 PSAO += *PaoloS(i,j);
		 }
	}
	
	}
	
	 
 }	
	aSTN = STN/SurfaceNT;
	aSTO = STO/SurfaceOT;
	aSAN = SAN/SurfaceNA;
	aSAO = SAO/SurfaceOA;
 //aSTN = PT*STN/SurfaceNT;
 //aSTO = PT*STO/SurfaceOT;
 //aSAN = PA*SAN/SurfaceNA;
 //aSAO = PA*SAO/SurfaceOA;
	
	PTN = PTN/countNT;
	PAN = PAN/countNA;
	PTO = PTO/countOT;
	PAO = PAO/countOA;
 
 aPSTN = PSTN/SurfaceNT;
 aPSTO = PSTO/SurfaceOT;
 aPSAN = PSAN/SurfaceNA;
 aPSAO = PSAO/SurfaceOA;
	
	ParticleSurfaceTN3<<STN<<"\t";
	ParticleSurfaceAN3<<SAN<<"\t";
	ParticleSurfaceTO3<<STO<<"\t";
	ParticleSurfaceAO3<<SAO<<"\t";
	
	aParticleSurfaceTN3<<aSTN<<"\t";
	aParticleSurfaceAN3<<aSAN<<"\t";
	aParticleSurfaceTO3<<aSTO<<"\t";
	aParticleSurfaceAO3<<aSAO<<"\t";
	
	PTN3<<PTN<<"\t";
	PAN3<<PAN<<"\t";
	PTO3<<PTO<<"\t";
	PAO3<<PAO<<"\t";
	
	PSTN3<<PSTN<<"\t";
	PSAN3<<PSAN<<"\t";
	PSTO3<<PSTO<<"\t";
	PSAO3<<PSAO<<"\t";

	
	aPSTN3<<aPSTN<<"\t";
	aPSAN3<<aPSAN<<"\t";
	aPSTO3<<aPSTO<<"\t";
	aPSAO3<<aPSAO<<"\t";	
 
 Pb = 10*Pb;
}

 ParticleSurfaceTN3<<endl;
 ParticleSurfaceAN3<<endl;
 ParticleSurfaceTO3<<endl;
 ParticleSurfaceAO3<<endl;
	aParticleSurfaceTN3<<endl;
	aParticleSurfaceAN3<<endl;
	aParticleSurfaceTO3<<endl;
	aParticleSurfaceAO3<<endl;
	
	PTN3<<endl;
	PAN3<<endl;
	PTO3<<endl;
	PAO3<<endl;
	
	PSTN3<<endl;
	PSAN3<<endl;
	PSTO3<<endl;
	PSAO3<<endl;

	
	aPSTN3<<endl;
	aPSAN3<<endl;
	aPSTO3<<endl;
	aPSAO3<<endl;
	
 Pa = 10*Pa;
}
 
ParticleSurfaceTN3.close();
ParticleSurfaceAN3.close();
ParticleSurfaceTO3.close();
ParticleSurfaceAO3.close();
	aParticleSurfaceTN3.close();
	aParticleSurfaceAN3.close();
	aParticleSurfaceTO3.close();
	aParticleSurfaceAO3.close();

PTN3.close();
PAN3.close();
PTO3.close();
PAO3.close();
PSTN3.close();
PSAN3.close();
PSTO3.close();
PSAO3.close();

aPSTN3.close();
aPSAN3.close();
aPSTO3.close();
aPSAO3.close();

	
/*****
double FAs = 0, FTs= 0, FAa=0, FTa=0, RA=0, RT=0, BT=0, BA=0, PT = 0, PA = 0;
sprintf( temp_string, "%s/FT_%d.txt", SaveDirectory,1);
     ofstream FLOWT( temp_string, ios::out );
	 
sprintf( temp_string, "%s/FA_%d.txt", SaveDirectory,1);
     ofstream FLOWA( temp_string, ios::out );

sprintf( temp_string, "%s/RT_%d.txt", SaveDirectory,1);
     ofstream RADIUST( temp_string, ios::out );
	 
sprintf( temp_string, "%s/RA_%d.txt", SaveDirectory,1);
     ofstream RADIUSA( temp_string, ios::out );
for( int i=0;i<Xnodes;i++)
 {
	for( int j=0;j<Ynodes;j++)
	{
		if(*BloodVesselIndicator(i,j)>0)
		{
		 if(*Phi(i,j)<0)
		 {
		 FTs += *Flow(i,j);
		 RT  += *BloodVesselRadius(i,j);		 
		 BT += 1;
		 }
		 FAs += *Flow(i,j);
		 RA  += *BloodVesselRadius(i,j);
		 BA += 1;
		}
	}
	
	 
 }
 
 for( int i=0;i<Xnodes;i++)
 {
	for( int j=0;j<Ynodes;j++)
	{
		
		 if(*Phi(i,j)<0)
		 {
			PT += 1;
		 }
		 else
		 {
		    PA += 1;
		 }
		 		
	}	
	 
 }
 FTa = FTs/BT;
 FAa = FAs/BA;
 RT = RT/BT;
 RA = RA/BA;		 
FLOWT<<FTs<<"\t";
FLOWT<<FTa<<"\t";
FLOWA<<TotalIn<<"\t";
FLOWA<<FAs<<"\t";
FLOWA<<FAa<<"\t";
RADIUST<<RT<<"\t";
RADIUST<<BT<<"\t";
RADIUSA<<RA<<"\t";
RADIUSA<<BA<<"\t";
FLOWT.close();
FLOWA.close();
RADIUST.close();
RADIUSA.close();
*******/
/****
double Pa, Pb, Pd;
double ST, SA;
Pd = 1.0e-7;

 sprintf( temp_string, "%s/ST_%d.txt", SaveDirectory,1);
     ofstream ParticleSurfaceT1( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/SA_%d.txt", SaveDirectory,1);
     ofstream ParticleSurfaceA1( temp_string, ios::out );
Pa = 2.95e5;

for(int ii=0;ii<6;ii++)
{

Pb = 6.63e-5;
for(int jj=0;jj<3;jj++)
{
UpdateTracer_P(Pa, Pb, Pd);
Transportation_P();
ST = 0.0;
SA = 0.0;
 for( int i=0;i<Xnodes;i++)
 {
	for( int j=0;j<Ynodes;j++)
	{
		 if(*Phi(i,j)<0)
		 {
		 ST += *TracerS(i,j);
		
		 }
		 SA += *TracerS(i,j);
		  cout<<*TracerS(i,j);
	}
	
	 
 }
 
 ParticleSurfaceT1<<ST<<"\t";
 ParticleSurfaceA1<<SA<<"\t";
 Pb = 10*Pb;
}

 ParticleSurfaceT1<<endl;
 ParticleSurfaceA1<<endl;
 Pa = 10*Pa;
}
 
ParticleSurfaceT1.close();
ParticleSurfaceA1.close();


Pd = 0.6e-6;
 sprintf( temp_string, "%s/ST_%d.txt", SaveDirectory,2);
     ofstream ParticleSurfaceT2( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/SA_%d.txt", SaveDirectory,2);
     ofstream ParticleSurfaceA2( temp_string, ios::out );
Pa = 2.95e5;

for(int ii=0;ii<6;ii++)
{

Pb = 6.63e-5;
for(int jj=0;jj<3;jj++)
{
UpdateTracer_P(Pa, Pb, Pd);
Transportation_P();
ST = 0.0;
SA = 0.0;
 for( int i=0;i<Xnodes;i++)
 {
	for( int j=0;j<Ynodes;j++)
	{
		 if(*Phi(i,j)<0)
		 {
		 ST += *TracerS(i,j);
		 }
		 SA += *TracerS(i,j);
	}
	
	 
 }
 
 ParticleSurfaceT2<<ST<<"\t";
 ParticleSurfaceA2<<SA<<"\t";
 Pb = 10*Pb;
}

 ParticleSurfaceT2<<endl;
 ParticleSurfaceA2<<endl;
 Pa = 10*Pa;
}
 
ParticleSurfaceT2.close();
ParticleSurfaceA2.close();



Pd = 1.0e-6;

 sprintf( temp_string, "%s/ST_%d.txt", SaveDirectory,3);
     ofstream ParticleSurfaceT3( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/SA_%d.txt", SaveDirectory,3);
     ofstream ParticleSurfaceA3( temp_string, ios::out );
Pa = 2.95e5;

for(int ii=0;ii<6;ii++)
{

Pb = 6.63e-5;
for(int jj=0;jj<3;jj++)
{
UpdateTracer_P(Pa, Pb, Pd);
Transportation_P();
ST = 0.0;
SA = 0.0;
 for( int i=0;i<Xnodes;i++)
 {
	for( int j=0;j<Ynodes;j++)
	{
		 if(*Phi(i,j)<0)
		 {
		 ST += *TracerS(i,j);
		 }
		 SA += *TracerS(i,j);
	}
	
	 
 }
 
 ParticleSurfaceT3<<ST<<"\t";
 ParticleSurfaceA3<<SA<<"\t";
 Pb = 10*Pb;
}

 ParticleSurfaceT3<<endl;
 ParticleSurfaceA3<<endl;
 Pa = 10*Pa;
}
 
ParticleSurfaceT3.close();
ParticleSurfaceA3.close();
***/




}
void VesselStructure::OutPutStudy_Area()
{
extern double TotalIn;
extern char SaveDirectory[1025];
 char temp_string[1024];
 extern Matrix Phi;
 extern int Xnodes, Ynodes;
 extern Matrix BloodVesselIndicator,Flow,BloodVesselRadius;
 extern Matrix TracerS;
 extern Matrix PreExist;
 extern double t;
	Matrix Paolo, PaoloS;
	Paolo = zeros(Xnodes, Ynodes);
	PaoloS = zeros(Xnodes, Ynodes);
//FIRST SUM, THEN AVERAGE
double FANs = 0, FAOs = 0, FTNs= 0,FTOs= 0, FANa=0,FAOa=0, FTNa=0,FTOa=0, RAO=0,RAN=0, RTN=0,RTO=0, BTN=0,BTO=0, BAN=0,BAO=0, PT = 0, PA = 0;
sprintf( temp_string, "%s/FT_%d_%f.txt", SaveDirectory,1,t);
     ofstream FLOWT( temp_string, ios::out );
	 
sprintf( temp_string, "%s/FA_%d_%f.txt", SaveDirectory,1,t);
     ofstream FLOWA( temp_string, ios::out );

sprintf( temp_string, "%s/RT_%d_%f.txt", SaveDirectory,1,t);
     ofstream RADIUST( temp_string, ios::out );
	 
sprintf( temp_string, "%s/RA_%d_%f.txt", SaveDirectory,1,t);
     ofstream RADIUSA( temp_string, ios::out );
for( int i=0;i<Xnodes;i++)
 {
	for( int j=0;j<Ynodes;j++)
	{
		
		if(*Phi(i,j)<0)
		 {
			PT += 1;
		 }
		 else
		 {
		    PA += 1;
		 }
		
		if(*BloodVesselIndicator(i,j)>0)
		{
		 if(*Phi(i,j)<0)
		 {		 
		 if(*PreExist(i,j)==0)
		 {
		 FTNs += *Flow(i,j);
		 RTN  += *BloodVesselRadius(i,j);		 
		 BTN += 1;
		 }
		 else
		 {
		 FTOs += *Flow(i,j);
		 RTO  += *BloodVesselRadius(i,j);		 
		 BTO += 1;
		 }
		 }
		 else
		 {
		 if(*PreExist(i,j)==0)
		 {
		 FANs += *Flow(i,j);
		 RAN  += *BloodVesselRadius(i,j);
		 BAN += 1;
		 }
		 else
		 {
		 FAOs += *Flow(i,j);
		 RAO  += *BloodVesselRadius(i,j);
		 BAO += 1;
		 }
		
		 }
	    }
		 		 
	}
 }
 PA = PA/10000;
 PT = PT/10000;

 FTNa = FTNs/BTN;
 FANa = FANs/BAN;
 FTOa = FTOs/BTO;
 FAOa = FAOs/BAO;
 RTN = RTN/BTN;
 RAN = RAN/BAN;
 RTO = RTO/BTO;
 RAO = RAO/BAO;		 
FLOWT<<FTNs<<"\t";
FLOWT<<FTOs<<"\t";
FLOWT<<FTNa<<"\t";
FLOWT<<FTOa<<"\t";
//FLOWA<<TotalIn<<"\t";
FLOWA<<FANs<<"\t";
FLOWA<<FAOs<<"\t";
FLOWA<<FANa<<"\t";
FLOWA<<FAOa<<"\t";

RADIUST<<RTN<<"\t";
RADIUST<<RTO<<"\t";
RADIUST<<BTN<<"\t";
RADIUST<<BTO<<"\t";
RADIUST<<PT<<"\t";
RADIUSA<<RAN<<"\t";
RADIUSA<<RAO<<"\t";
RADIUSA<<BAN<<"\t";
RADIUSA<<BAO<<"\t";
RADIUSA<<PA<<"\t";
FLOWT.close();
FLOWA.close();
RADIUST.close();
RADIUSA.close();

double Pa, Pb, Pd;
double STN,STO, SAN,SAO;
Pd = 1.0e-7;

 sprintf( temp_string, "%s/STN_%d_%f.txt", SaveDirectory,1,t);
     ofstream ParticleSurfaceTN1( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/SAN_%d_%f.txt", SaveDirectory,1,t);
     ofstream ParticleSurfaceAN1( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/STO_%d_%f.txt", SaveDirectory,1,t);
     ofstream ParticleSurfaceTO1( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/SAO_%d_%f.txt", SaveDirectory,1,t);
     ofstream ParticleSurfaceAO1( temp_string, ios::out );

Pa = 2.95e5;

for(int ii=0;ii<6;ii++)
{

Pb = 6.63e-5;
for(int jj=0;jj<3;jj++)
{
UpdateTracer_P(Pa, Pb, Pd, Paolo, PaoloS);
Transportation_P();
STN = 0.0;
SAN = 0.0;
STO = 0.0;
SAO = 0.0;
 for( int i=0;i<Xnodes;i++)
 {
	for( int j=0;j<Ynodes;j++)
	{
	
	if(*BloodVesselIndicator(i,j)>0)
	{
		 if(*Phi(i,j)<0&&*PreExist(i,j)==0)
		 {
		 STN += *TracerS(i,j);
		
		 }
		 else if(*Phi(i,j)<0&&*PreExist(i,j)>0)
		 {
		 STO += *TracerS(i,j);
		 }
		 else if(*Phi(i,j)>0&&*PreExist(i,j)==0)
		 {
		 SAN += *TracerS(i,j);
		
		 }
		 else if(*Phi(i,j)>0&&*PreExist(i,j)>0)
		 {
		 SAO += *TracerS(i,j);
		 }
	}
	
	}
	
	 
 }
 STN = STN/PT;
 STO = STO/PT;
 SAN = SAN/PA;
 SAO = SAO/PA;
 
 ParticleSurfaceTN1<<STN<<"\t";
 ParticleSurfaceAN1<<SAN<<"\t";
 ParticleSurfaceTO1<<STO<<"\t";
 ParticleSurfaceAO1<<SAO<<"\t";
 Pb = 10*Pb;
}

 ParticleSurfaceTN1<<endl;
 ParticleSurfaceAN1<<endl;
 ParticleSurfaceTO1<<endl;
 ParticleSurfaceAO1<<endl;
 Pa = 10*Pa;
}
 
ParticleSurfaceTN1.close();
ParticleSurfaceAN1.close();
ParticleSurfaceTO1.close();
ParticleSurfaceAO1.close();


Pd = 0.6e-6;
 sprintf( temp_string, "%s/STN_%d_%f.txt", SaveDirectory,2,t);
     ofstream ParticleSurfaceTN2( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/SAN_%d_%f.txt", SaveDirectory,2,t);
     ofstream ParticleSurfaceAN2( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/STO_%d_%f.txt", SaveDirectory,2,t);
     ofstream ParticleSurfaceTO2( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/SAO_%d_%f.txt", SaveDirectory,2,t);
     ofstream ParticleSurfaceAO2( temp_string, ios::out );

Pa = 2.95e5;

for(int ii=0;ii<6;ii++)
{

Pb = 6.63e-5;
for(int jj=0;jj<3;jj++)
{
UpdateTracer_P(Pa, Pb, Pd, Paolo, PaoloS);
Transportation_P();
STN = 0.0;
SAN = 0.0;
STO = 0.0;
SAO = 0.0;
 for( int i=0;i<Xnodes;i++)
 {
	for( int j=0;j<Ynodes;j++)
	{
	
	if(*BloodVesselIndicator(i,j)>0)
	{
	
		 if(*Phi(i,j)<0&&*PreExist(i,j)==0)
		 {
		 STN += *TracerS(i,j);
		
		 }
		 else if(*Phi(i,j)<0&&*PreExist(i,j)>0)
		 {
		 STO += *TracerS(i,j);
		 }
		 else if(*Phi(i,j)>0&&*PreExist(i,j)==0)
		 {
		 SAN += *TracerS(i,j);
		
		 }
		 else if(*Phi(i,j)>0&&*PreExist(i,j)>0)
		 {
		 SAO += *TracerS(i,j);
		 }
		 
	}	 
	}
	
	 
 }
 STN = STN/PT;
 STO = STO/PT;
 SAN = SAN/PA;
 SAO = SAO/PA;
  
ParticleSurfaceTN2<<STN<<"\t";
 ParticleSurfaceAN2<<SAN<<"\t";
 ParticleSurfaceTO2<<STO<<"\t";
 ParticleSurfaceAO2<<SAO<<"\t";
 Pb = 10*Pb;
}

 ParticleSurfaceTN2<<endl;
 ParticleSurfaceAN2<<endl;
 ParticleSurfaceTO2<<endl;
 ParticleSurfaceAO2<<endl;
 Pa = 10*Pa;
}
 
ParticleSurfaceTN2.close();
ParticleSurfaceAN2.close();
ParticleSurfaceTO2.close();
ParticleSurfaceAO2.close();



Pd = 1.0e-6;

 sprintf( temp_string, "%s/STN_%d_%f.txt", SaveDirectory,3,t);
     ofstream ParticleSurfaceTN3( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/SAN_%d_%f.txt", SaveDirectory,3,t);
     ofstream ParticleSurfaceAN3( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/STO_%d_%f.txt", SaveDirectory,3,t);
     ofstream ParticleSurfaceTO3( temp_string, ios::out );
	 
 sprintf( temp_string, "%s/SAO_%d_%f.txt", SaveDirectory,3,t);
     ofstream ParticleSurfaceAO3( temp_string, ios::out );
Pa = 2.95e5;

for(int ii=0;ii<6;ii++)
{

Pb = 6.63e-5;
for(int jj=0;jj<3;jj++)
{
UpdateTracer_P(Pa, Pb, Pd, Paolo, PaoloS);
Transportation_P();
STN = 0.0;
SAN = 0.0;
STO = 0.0;
SAO = 0.0;
 for( int i=0;i<Xnodes;i++)
 {
	for( int j=0;j<Ynodes;j++)
	{
	
	if(*BloodVesselIndicator(i,j)>0)
	{
		 if(*Phi(i,j)<0&&*PreExist(i,j)==0)
		 {
		 STN += *TracerS(i,j);
		
		 }
		 else if(*Phi(i,j)<0&&*PreExist(i,j)>0)
		 {
		 STO += *TracerS(i,j);
		 }
		 else if(*Phi(i,j)>0&&*PreExist(i,j)==0)
		 {
		 SAN += *TracerS(i,j);
		
		 }
		 else if(*Phi(i,j)>0&&*PreExist(i,j)>0)
		 {
		 SAO += *TracerS(i,j);
		 }
	}
	}
	
	 
 }
 STN = STN/PT;
 STO = STO/PT;
 SAN = SAN/PA;
 SAO = SAO/PA;
 
 ParticleSurfaceTN3<<STN<<"\t";
 ParticleSurfaceAN3<<SAN<<"\t";
 ParticleSurfaceTO3<<STO<<"\t";
 ParticleSurfaceAO3<<SAO<<"\t";
 Pb = 10*Pb;
}

 ParticleSurfaceTN3<<endl;
 ParticleSurfaceAN3<<endl;
 ParticleSurfaceTO3<<endl;
 ParticleSurfaceAO3<<endl;
 Pa = 10*Pa;
}
 
ParticleSurfaceTN3.close();
ParticleSurfaceAN3.close();
ParticleSurfaceTO3.close();
ParticleSurfaceAO3.close();




}

void VesselStructure::MindeUpdateHematocrit()
{
//cout <<"function UpdateHematocrit running"<<endl; 

 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodNodePressure;
 extern Matrix BloodVesselPressureX;
 extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
 extern Matrix FlowX, FlowY;
 extern Matrix WssX, WssY;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix ConductX, ConductY; //conductivity
 extern int Xnodes, Ynodes;
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;


    double FlowMin=1.0e-30;
	double TimeStepMax= AngioDeltaT;
	int MinTime_i,MinTime_j;
	double pi=3.141592654;
	double TimeStep = AngioDeltaT;
	double TimeFactor;
	TimeFactor = 1.0;
	double MaxHt = RetrieveDoubleParameter( ParameterFile , "MaxHt");
	double AverageHt = RetrieveDoubleParameter( ParameterFile , "AverageHt");
	Matrix Flow;
	Matrix PassByMass;
	Flow = zeros( Xnodes, Ynodes);
	PassByMass = zeros( Xnodes, Ynodes);
	int i_minus, j_minus, i_plus, j_plus;
	int i, j;
	while(pCurrentVesselPoint)
	{
		 i = pCurrentVesselPoint->i;
		 j = pCurrentVesselPoint->j;
		
		//cout<<"here after"<<endl;
				//coue<"here before"<<endl;
		
		//cout<<*Flow(i,j)<<endl;
		//cout<<min_abs( *FlowX(i, j) )<<endl;
		
	//	cout<<*FlowX(i,j)<<endl;
	//	cout<<*FlowY(i,j)<<endl;
		if(min_abs( *FlowX(i, j) ) > FlowMin)
		{
			TimeStep = pi * pow( *BloodVesselRadiusXdir(i, j), 2) * NodeLength / min_abs( *FlowX(i, j) );
			//cout<<"TimeStep"<<TimeStep<<endl;
			if (TimeStep < TimeStepMax)
			{
				TimeStepMax = TimeStep;
				//cout<<"TimeStepMax"<<TimeStepMax<<endl;
			}
			
		}
		
		if(min_abs( *FlowY(i, j) ) > FlowMin)
		{
			TimeStep = pi * pow( *BloodVesselRadiusYdir(i, j), 2) * NodeLength / min_abs( *FlowY(i, j) );
			if (TimeStep < TimeStepMax)
			{
				TimeStepMax = TimeStep;
			}

		}
	  
	  pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
	}
	
	AngioDeltaT = TimeStepMax / TimeFactor;
	cout<<"AngioDeltaT"<<AngioDeltaT<<endl;
	
	pCurrentVesselPoint = pFirstVesselPoint;
	
	//get bypassmass
	while(pCurrentVesselPoint)
	{
		 i = pCurrentVesselPoint->i;
		 j = pCurrentVesselPoint->j;
		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if(*FlowX(i, j) > 0&&(min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{       
			*Flow(i, j) += min_abs( *FlowX(i, j) );

			*PassByMass(i, j) += min_abs( *FlowX(i, j) ) * *HaematocritLevel(i_minus, j) ;
			//cout<<*PassByMass(i, j)<<"ByMass"<<endl; 
	}
	
	if(*FlowX(i_plus, j)*(-1.0) > 0&&(min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
			*Flow(i, j) += min_abs( *FlowX(i_plus, j) );
			*PassByMass(i, j) += min_abs( *FlowX(i_plus, j) ) * *HaematocritLevel(i_plus, j) ;
			//cout<<*PassByMass(i, j)<<"ByMass"<<endl; 
	}
	
	if(*FlowY(i, j) > 0&&(min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
			*Flow(i, j) += min_abs( *FlowY(i, j) );
			*PassByMass(i, j) += min_abs( *FlowY(i, j) ) * *HaematocritLevel(i, j_minus) ;
			//cout<<*PassByMass(i, j)<<"ByMass"<<endl; 
	}
	
	if(*FlowY(i, j_plus)*(-1.0) > 0&&(min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
			*Flow(i, j) += min_abs( *FlowY(i, j_plus) );
			*PassByMass(i, j) += min_abs( *FlowY(i, j_plus) ) * *HaematocritLevel(i, j_plus);
			//cout<<*PassByMass(i, j)<<"ByMass"<<endl; 
			
	}
		
	

	
	pCurrentVesselPoint = pCurrentVesselPoint -> pNextVesselPoint;
	}
	
	/**********
		ofstream myfile100;
	 myfile100.open ("PassByMass.txt");
  for( int iii=0 ; iii < Xnodes ; iii++ )
 {
  for( int jjj=0 ; jjj < Ynodes ; jjj++ )
  {
  myfile100 <<*PassByMass(iii,jjj)<<"   ";
  }
  myfile100<< endl<<endl;
}
myfile100<< endl<<endl;
  myfile100.close();
  ***********/

	
     
	//Compute Hematocrit
	pCurrentVesselPoint = pFirstVesselPoint;
	
	
	while(pCurrentVesselPoint)
	{
	     i = pCurrentVesselPoint->i;
		 j = pCurrentVesselPoint->j;
			
	if(pCurrentVesselPoint->AmIArtery)
	{
		//*HemX(i, j) =0.45;//+= min_abs(*FlowX(i, j) * AngioDeltaT 
		            //        / ( pi * pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength )
					//	    * ( TimeFactor * AverageHt - TimeFactor * *HemX(i, j) ) / MaxHt) ;
		*HaematocritLevel(i, j) =0.45;//+= min_abs(*FlowY(i, j) * AngioDeltaT 
		              //      / ( pi * pow( *BloodVesselRadiusYdir(i, j), 2 ) * NodeLength )
						//    * ( TimeFactor * AverageHt - TimeFactor * *HemY(i, j) ) / MaxHt) ;	
							//cout<<*FlowX(i, j)<<"  "<<AngioDeltaT<<"  "<<pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength<<"  "<<( TimeFactor * AverageHt - TimeFactor * *HemX(i, j) )<<endl;
							//cout<<*HemX(i, j)<<"   "<<*HemY(i, j)<<endl;
	}
	else
	{
		if(*Flow(i, j) > FlowMin)
		{		
		
				
		*HaematocritLevel(i, j) += min_abs(AngioDeltaT * TimeFactor/( pi * pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength ))/ MaxHt
							* ( *PassByMass(i, j)  -  *Flow(i, j)* *HaematocritLevel(i, j) )  ;
			
									//cout<<*HaematocritLevel(i, j)<<endl;							
	 
		
		}
	}
	
	if( *HaematocritLevel(i, j) > 1.0 )
	{
		*HaematocritLevel(i, j) = 1.0;
	}
	else if( *HaematocritLevel(i, j) < 0.0 )
	{
		*HaematocritLevel(i, j) = 0.0;
		//cout<<"Something is wrong! or not conserved"<<endl;
	}
	
	pCurrentVesselPoint = pCurrentVesselPoint -> pNextVesselPoint;
	}
	
	  
cout <<"function UpdateHematocrit finished"<<endl; 
}


/*
double VesselStructure::PTUpdate_Wave(Matrix& CoeffecientX, Matrix& CoeffecientY, Matrix& Variable,int i,int j)
{

 extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
 extern Matrix ConductX, ConductY;
 extern int Xnodes, Ynodes;

 double Output=0.0;
 double PassByMass=0;
 double Coeffecient=0;
 int i_minus, j_minus, i_plus, j_plus;

 

		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if(*CoeffecientX(i, j)/ *ConductX(i,j) >0.001 &&(min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{       
			Coeffecient += min_abs( *CoeffecientX(i, j));

			PassByMass += min_abs( *CoeffecientX(i, j) ) * *Variable(i_minus, j) ;
			//cout<<PassByMass<<"ByMass"<<endl; 
	}
	
	if(*CoeffecientX(i_plus, j)*(-1.0)/ *ConductX(i,j) > 0.001&&(min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
			Coeffecient += min_abs( *CoeffecientX(i_plus, j) );
			PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * *Variable(i_plus, j) ;
			//cout<<PassByMass<<"ByMass"<<endl; 
	}
	
	if(*CoeffecientY(i, j)/ *ConductY(i,j) > 0.001&&(min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
			Coeffecient += min_abs( *CoeffecientY(i, j) );
			PassByMass += min_abs( *CoeffecientY(i, j) ) * *Variable(i, j_minus) ;
			//cout<<PassByMass<<"ByMass"<<endl; 
	}
	
	if(*CoeffecientY(i, j_plus)*(-1.0)/ *ConductY(i,j) > 0.001&&(min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
			Coeffecient += min_abs( *CoeffecientY(i, j_plus) );
			PassByMass += min_abs( *CoeffecientY(i, j_plus) ) * *Variable(i, j_plus);
			//cout<<PassByMass<<"ByMass"<<endl; 
			
	}
	
	if(Coeffecient>0)
	{
	Output=(PassByMass)/(Coeffecient);
	}
	else
	{
	Output=*Variable(i,j);
	}	
	 
		
	return Output;
}
*/

double VesselStructure::Transport_P(Matrix& CoeffecientX, Matrix& CoeffecientY, Matrix& Variable,int i,int j,double dt)
{

 extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
 extern Matrix BloodVesselRadiusXdir,BloodVesselRadiusYdir;
 extern Matrix TracerX, TracerY;
 extern int Xnodes, Ynodes;

 double pi=3.141592654;
 double Output=0.0;
 double PassByMass=0;
 double Coeffecient=0;
 int i_minus, j_minus, i_plus, j_plus;

 

		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{    
	       if(*CoeffecientX(i, j)>0)
			{			
			Coeffecient +=  pi * pow( *BloodVesselRadiusXdir(i, j), 2);
			PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j)*(1-*TracerX(i,j)) - *Variable(i, j));			
			}
	
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
	       if(*CoeffecientX(i_plus, j)<0)
			{
			Coeffecient +=  pi * pow( *BloodVesselRadiusXdir(i_plus, j), 2);
			PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * (*Variable(i_plus, j)*(1-*TracerX(i_plus,j)) - *Variable(i, j));
			}
	
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
		if(*CoeffecientY(i, j)>0)
			{
			Coeffecient += pi * pow( *BloodVesselRadiusYdir(i, j), 2);
			PassByMass += min_abs( *CoeffecientY(i, j) ) * (*Variable(i, j_minus)*(1-*TracerY(i,j)) - *Variable(i, j));	
			}
		
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
		
		if(*CoeffecientY(i, j_plus)<0)
			{
			Coeffecient +=  pi * pow( *BloodVesselRadiusYdir(i, j_plus), 2);	
			PassByMass += min_abs( *CoeffecientY(i, j_plus) ) * (*Variable(i, j_plus)*(1-*TracerY(i,j_plus)) - *Variable(i, j));
			//cout<<PassByMass<<"ByMass"<<endl; 
			}
	}

	
	if(Coeffecient>0)
	{
	Output = *Variable(i,j) + (dt/NodeLength)*(PassByMass)/(Coeffecient);
	//cout<<PassByMass<<endl;
	if(Output<0)
	{
	//cout<<PassByMass<<endl;
	Output = 0.0;
	}
	
	}
	else
	{
	Output=0.0;
	}	
	
		
	return Output;
}


		
 double VesselStructure::Transport_DI(Matrix& CoeffecientX, Matrix& CoeffecientY, Matrix& Variable, Matrix& VariableS,Matrix& Fraction, int i,int j,double dt)
{
	
	extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
	extern Matrix BloodVesselRadiusXdir,BloodVesselRadiusYdir;
	extern Matrix Leak;
	extern int Xnodes, Ynodes;
	extern double Kwall1;
	extern double Kwall2;
	extern Matrix Conduct;
	//extern Matrix TracerS;
	double pi=3.141592654;
	double Output=0.0;
	double PassByMass=0;
	
	double Coeffecient=0;
	double Vp = 0;
	double Vt = 1e-15;
	double CoeffAv= *Conduct(i,j)*1e-15;//unit in m^2
	int i_minus, j_minus, i_plus, j_plus;
	
		
	
	if(j == Ynodes-1)// boundary condition again
	{
		j_plus = Ynodes-1;
		j_minus= j-1;
	}
	else if( j == 0)
	{
		j_minus = 0;
		j_plus=j+1;
	}
	else
	{
		j_plus = j+1;
		j_minus = j-1;
	}
	
	if(i == Xnodes-1)// boundary condition again NOTES
	{
		i_plus = Xnodes-1;
		i_minus= i-1;
	}
	
	else if( i == 0)
	{
		i_minus =0;
		i_plus = i+1;
	}
	else
	{
		i_plus = i+1;
		i_minus = i-1;
	}
	
	
	
	
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{
		if(*CoeffecientX(i, j)>0)
		{		
			Vp +=  pi * pow( *BloodVesselRadiusXdir(i, j), 2) * NodeLength;
			PassByMass += min_abs( *CoeffecientX(i, j) ) * *Variable(i_minus, j);
			Coeffecient +=  min_abs( *CoeffecientX(i, j) ) ;
			
		}
	}
	
	
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
		if(*CoeffecientX(i_plus, j)<0)
		{
			Vp +=  pi * pow( *BloodVesselRadiusXdir(i_plus, j), 2)* NodeLength;
			PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * *Variable(i_plus, j);
			Coeffecient +=  min_abs( *CoeffecientX(i_plus, j) ) ;
	    }
    }
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
		if(*CoeffecientY(i, j)>0)
	    {
			Vp += pi * pow( *BloodVesselRadiusYdir(i, j), 2)* NodeLength;
			PassByMass += min_abs( *CoeffecientY(i, j) ) * *Variable(i, j_minus);
			Coeffecient +=  min_abs( *CoeffecientY(i, j) ) ;

			
		}
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
		if(*CoeffecientY(i, j_plus)<0)
        {
			Vp +=  pi * pow( *BloodVesselRadiusYdir(i, j_plus), 2)* NodeLength;	
			PassByMass += min_abs( *CoeffecientY(i, j_plus) )* *Variable(i, j_plus);
			Coeffecient +=   min_abs( *CoeffecientY(i, j_plus) );
			//cout<<PassByMass<<"ByMass"<<endl; 
		}
	}
	
	//if(Vp==0)
	//{
		//cout<<" i: "<<i<<" j: "<<j<<" Vp "<< Vp <<" Flow "<< *Variable(i,j)<<" uP  "<<PassByMass<<" Coeffecient "<<Coeffecient;
	//	cout<<" i: "<<i<<" j: "<<j<<" Vp "<< Vp<<endl;
	//	cout<<"  "<< *CoeffecientX(i, j)<<"  "<< *CoeffecientX(i_plus, j) <<"  "<< *CoeffecientY(i, j) <<"  "<< *CoeffecientY(i, j_plus) <<endl;  
	//	cout<<"  "<< *BloodVesselRadiusXdir(i, j)<<"  "<< *BloodVesselRadiusXdir(i_plus, j)<<"  "<<  *BloodVesselRadiusYdir(i, j)<<"  "<< *BloodVesselRadiusYdir(i, j_plus)<<endl;  
	//	getchar();
	//}
	
	if(Coeffecient!=0&&Vp!=0)
	{
	Coeffecient *=dt;
	Coeffecient /=Vp;
	Coeffecient += 1;
	PassByMass *=dt;
	PassByMass /=Vp;
	PassByMass += *Variable(i,j);
	
	double J = dt/Vp *(Kwall2*CoeffAv* *VariableS(i,j) - Kwall1*CoeffAv* *Variable(i,j)- *Leak(i,j)*1e-18* *Variable(i,j));
	
	PassByMass += J;	
		
		
	
	//if(i==200||j==200)
	//{
	//	cout<<i<<"  "<<j<<" "<< Vp;
	//	cout<<"  pM"<<PassByMass<<"  Coef"<<Coeffecient<<" Variable"<< *Variable(i,j)<<"  "<< *Variable(i,j_minus)<<"  "<<endl;
	//	cout<<"  pM"<<PassByMass<<"  Coef"<<Coeffecient<<" Variable"<< *Variable(i,j)<<"  "<< *Variable(i_minus,j)<<"  "<<endl;
	//}

	
	Output = PassByMass/Coeffecient;
	*Fraction(i,j)= Output*Vp;
	//if(!(abs(Output)<1.0e20))
	//{
		//cout<<" i: "<<i<<" j: "<<j<<" Vp "<< Vp <<" Flow "<< *Variable(i,j)<<" uP  "<<PassByMass<<" Coeffecient "<<Coeffecient;
	//	cout<<" i: "<<i<<" j: "<<j<<" Vp "<< Vp ;
	//	getchar();
	//}
	
	
	if(Output<0)
	{
		Output = 0;	
		*Fraction(i,j)= 0;
	}
		
	}	
	
	
	return Output;
}
				   
double DRGsinkFunctionBreak(double DRG, int i,int j)
{
	extern double _EPS;
	
	extern Matrix BloodVesselIndicator;
	extern Matrix TracerS;
	extern Matrix Phi, PhiNecrotic;
	extern Matrix Oxygen;
	extern Matrix Drainage;
	extern double TracerMax;
	extern Matrix PreExist;
	extern double DRGprodctionConstant;
	extern double DRGdecayConstant;
	extern double DRG_DegradationConstant;
	extern double Kwall2;
	double output = 0.0;
	
	// no interpolation is needed because source is only called at node points
	//if(*TracerT(i,j)>DRG)
	
	
	
	output -= (DRGdecayConstant+ *Drainage(i,j)/1000);
	
	
	//if( *BloodVesselIndicator(i,j)>0 )
	//{ 
	//	output -= Kwall2;
	//} // MDEprodctionConstant; }
	
	
	if( *Phi(i,j) <= _EPS && *PhiNecrotic(i,j) > _EPS )
	{ 
		output -= DRG_DegradationConstant;
	} // MDEprodctionConstant; }
	
	
	
	return output;
}

double ImplicitConvectUpdate(
							 Matrix& Diffusion, Matrix& Source, Matrix& PreviousTimeStep,
 double (*SourceTimesU)(double,int,int), int i, int j, double dt)

{
	extern Matrix InterPressure;
	
	extern double AdvectionConstant;
	double dx = 10;//mum
	double dy = 10;//mum
	double dx2 = square(dx);
	double dy2 = square(dy);
	extern int Xnodes,Ynodes;
	//double Uij = *Solution(i,j);
	//double Uim1_j = *Solution(i-1,j);
	//double Uip1_j = *Solution(i+1,j);
	//double Ui_jm1 = *Solution(i,j-1);
	//double Ui_jp1 = *Solution(i,j+1);
	
	double Uij = *PreviousTimeStep(i,j);
	double Uim1_j = *PreviousTimeStep(i-1,j);
	double Uip1_j = *PreviousTimeStep(i+1,j);
	double Ui_jm1 = *PreviousTimeStep(i,j-1);
	double Ui_jp1 = *PreviousTimeStep(i,j+1);
	double u = *PreviousTimeStep(i,j);
	
	double CenterDiffusion = *Diffusion(i,j);
	
	double CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
	double CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
	double CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
	double CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
	
	
	double SrcLin = SourceTimesU(Uij,i,j);
	double SrcNonLin = *Source(i,j);

	double PassbyMass = ( u/dt + (CoeffXell*Uim1_j + CoeffXr*Uip1_j)/dx2 
						 + (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)/dy2 + SrcNonLin/dt );
	
	double Coeffecient = ( 1.0/dt + (CoeffXell+CoeffXr)/dx2 + (CoeffYell+CoeffYr)/dy2 - SrcLin );
  
	//double Pxx = (*PressureGradientX(i+1,j)-*PressureGradientX(i-1,j))/(2*dx);
	//double Pyy = (*PressureGradientY(i,j+1)-*PressureGradientY(i,j-1))/(2*dy);
   	
	double Px_i_m1_j = 0;
	double Px_i_p1_j = 0;
	
	if(i==1)
	{
		 Px_i_m1_j = 0;
		 Px_i_p1_j = (*InterPressure(i+2,j)- *InterPressure(i,j))/(2*dx);
	}
	else if(i==Xnodes-2)
	{
		 Px_i_m1_j = (*InterPressure(i,j)- *InterPressure(i-2,j))/(2*dx);
		 Px_i_p1_j = 0;
	}
	else 
	{
		 Px_i_m1_j = (*InterPressure(i,j)- *InterPressure(i-2,j))/(2*dx);
		 Px_i_p1_j = (*InterPressure(i+2,j)- *InterPressure(i,j))/(2*dx);
	}

	
	double Py_i_j_m1 = 0;
	double Py_i_j_p1 = 0;
	
	if(j==1)
	{
		 Py_i_j_m1 = 0;
		 Py_i_j_p1 = (*InterPressure(i,j+2)- *InterPressure(i,j))/(2*dy);
	}
	else if(j==Ynodes-2)
	{
		 Py_i_j_m1 = (*InterPressure(i,j)- *InterPressure(i,j-2))/(2*dy);
		 Py_i_j_p1 = 0;
	}
	else 
	{
		 Py_i_j_m1 = (*InterPressure(i,j)- *InterPressure(i,j-2))/(2*dy);
		 Py_i_j_p1 = (*InterPressure(i,j+2)- *InterPressure(i,j))/(2*dy);
	}
	
	
	double Px_i_j = (*InterPressure(i+1,j)- *InterPressure(i-1,j))/(2*dx);
    double Py_i_j = (*InterPressure(i,j+1)- *InterPressure(i,j-1))/(2*dy);
	
	double Pxxyy = (Px_i_p1_j - Px_i_m1_j)/(2*dx) + (Py_i_j_p1 - Py_i_j_m1)/(2*dy);
	
		Coeffecient -= AdvectionConstant*Pxxyy;
	
	
	
	
	
	if(Px_i_j<0)
	
	{
		PassbyMass -= AdvectionConstant* Px_i_j * Uim1_j/dx;
		Coeffecient -= AdvectionConstant* Px_i_j/dx;
		
	}
	else if(Px_i_j>0)
	{
		PassbyMass += AdvectionConstant* Px_i_j* Uip1_j/dx;
		Coeffecient += AdvectionConstant* Px_i_j/dx;

	}
	
	
	if(Py_i_j<0)
		
	{
		PassbyMass -= AdvectionConstant* Py_i_j * Ui_jm1/dy;
		Coeffecient -= AdvectionConstant* Py_i_j/dy;	
	}
	else if(Py_i_j>0)
	{
		PassbyMass += AdvectionConstant* Py_i_j * Ui_jp1/dy;
		Coeffecient += AdvectionConstant* Py_i_j/dy;	
	}
		
	
	
	
	
	
	
	
	
	return PassbyMass/Coeffecient;
	
	
	
	
	
	
	
	
}

double ImplicitConvectUpdateBoundaryPoint(
							 Matrix& Diffusion, Matrix& Source, Matrix& PreviousTimeStep, 
							 double (*SourceTimesU)(double,int,int), int i, int j,double dt)

{   extern double dx;
	extern double dy;
	double dx2 = square(dx);
	double dy2 = square(dy);
	
	double u = *PreviousTimeStep(i,j);
	
	double CenterDiffusion = *Diffusion(i,j);
	
	
	
    double Uij = 0;
	double Uim1_j = 0;
	double Uip1_j = 0;
	double Ui_jm1 = 0;
	double Ui_jp1 = 0;
	
	
	
	double CoeffXell = 0;
	double CoeffXr   = 0;
	double CoeffYell = 0;
	double CoeffYr   = 0;
	
	
	
	if(i==0)
	{
		if(j == 0)
		{
			
			Uim1_j = *PreviousTimeStep(i+1,j);
			Uip1_j = *PreviousTimeStep(i+1,j);
			Ui_jm1 = *PreviousTimeStep(i,j+1);
			Ui_jp1 = *PreviousTimeStep(i,j+1);
			
			
			CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			
			
		}
		else if(j == PreviousTimeStep.TellCols() - 1)
		{
			Uim1_j = *PreviousTimeStep(i+1,j);
			Uip1_j = *PreviousTimeStep(i+1,j);
			Ui_jm1 = *PreviousTimeStep(i,j-1);
			Ui_jp1 = *PreviousTimeStep(i,j-1);
			
			
			CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			
			
		}
		else
		{
			
			Uim1_j = *PreviousTimeStep(i+1,j);
			Uip1_j = *PreviousTimeStep(i+1,j);
			Ui_jm1 = *PreviousTimeStep(i,j-1);
			Ui_jp1 = *PreviousTimeStep(i,j+1);
			
			
			CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			
			
		}
	}
	
	if(i==PreviousTimeStep.TellRows() - 1)
	{
		if(j == 0)
		{
			Uim1_j = *PreviousTimeStep(i-1,j);
			Uip1_j = *PreviousTimeStep(i-1,j);
			Ui_jm1 = *PreviousTimeStep(i,j+1);
			Ui_jp1 = *PreviousTimeStep(i,j+1);
			
			CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			
		}
		else if(j == PreviousTimeStep.TellCols() - 1)
		{
			Uim1_j = *PreviousTimeStep(i-1,j);
			Uip1_j = *PreviousTimeStep(i-1,j);
			Ui_jm1 = *PreviousTimeStep(i,j-1);
			Ui_jp1 = *PreviousTimeStep(i,j-1);
			
			CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			
		}
		else
		{
			Uim1_j = *PreviousTimeStep(i-1,j);
			Uip1_j = *PreviousTimeStep(i-1,j);
			Ui_jm1 = *PreviousTimeStep(i,j-1);
			Ui_jp1 = *PreviousTimeStep(i,j+1);
			
			CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			
		}
	}
	
	
	if(j==0)
	{
		if(i == 0)
		{
			//done
			Uim1_j = *PreviousTimeStep(i+1,j);
			Uip1_j = *PreviousTimeStep(i+1,j);
			Ui_jm1 = *PreviousTimeStep(i,j+1);
			Ui_jp1 = *PreviousTimeStep(i,j+1);
			
			
			CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			
		}
		else if(i == PreviousTimeStep.TellRows() - 1)
		{
			//done
			Uim1_j = *PreviousTimeStep(i-1,j);
			Uip1_j = *PreviousTimeStep(i-1,j);
			Ui_jm1 = *PreviousTimeStep(i,j+1);
			Ui_jp1 = *PreviousTimeStep(i,j+1);
			
			CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			
		}
		else
		{
			Uim1_j = *PreviousTimeStep(i-1,j);
			Uip1_j = *PreviousTimeStep(i+1,j);
			Ui_jm1 = *PreviousTimeStep(i,j+1);
			Ui_jp1 = *PreviousTimeStep(i,j+1);
			
			CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			
		}
	}
	
	if(j==PreviousTimeStep.TellCols() - 1)
	{
		if(i == 0)
		{
			//done
			Uim1_j = *PreviousTimeStep(i+1,j);
			Uip1_j = *PreviousTimeStep(i+1,j);
			Ui_jm1 = *PreviousTimeStep(i,j-1);
			Ui_jp1 = *PreviousTimeStep(i,j-1);
			
			
			CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
		}
		else if(i == PreviousTimeStep.TellRows() - 1)
		{
			//done
			Uim1_j = *PreviousTimeStep(i-1,j);
			Uip1_j = *PreviousTimeStep(i-1,j);
			Ui_jm1 = *PreviousTimeStep(i,j-1);
			Ui_jp1 = *PreviousTimeStep(i,j-1);
			
			CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
		}
		else
		{
			Uim1_j = *PreviousTimeStep(i-1,j);
			Uip1_j = *PreviousTimeStep(i+1,j);
			Ui_jm1 = *PreviousTimeStep(i,j-1);
			Ui_jp1 = *PreviousTimeStep(i,j-1);
			
			CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			
		}
	}
	
	
	
	
	
		
	
	
	
	
	
	
	
	
	
	double SrcLin = SourceTimesU(Uij,i,j);
	double SrcNonLin = *Source(i,j);
	
	return ( u/dt + (CoeffXell*Uim1_j + CoeffXr*Uip1_j)/dx2 
			+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)/dy2 + SrcNonLin/dt )
	/( 1.0/dt + (CoeffXell+CoeffXr)/dx2 + (CoeffYell+CoeffYr)/dy2 - SrcLin );
	
	/*
	 else{
	 N = Yoffset*j + i;
	 x = *X(i);
	 y = *Y(j);
	 u = *PreviousTimeStep(i,j); 
	 
	 double DiffusionLeft  = DiffusionConstant(u,x-0.5*dx,y,i,j);
	 double DiffusionRight = DiffusionConstant(u,x+0.5*dx,y,i,j);
	 
	 *Coefficients.e(N,N-1) += (DiffusionLeft)*ConstX;
	 *Coefficients.e(N,N+1) += (DiffusionRight)*ConstX;
	 *Coefficients.e(N,N) -= 1.0;
	 *Coefficients.e(N,N) -= ConstX*(DiffusionLeft+DiffusionRight);
	 
	 *RHS(N) -= dt*Source(u,x,y,i,j);
	 if(*BloodVesselIndicator(i,j)>0)
	 {
	 *RHS(N) -= (*AnotherSource(i,j)-u);
	 }
	 
	 *RHS(N) -= u;			
	 *RHS(N) += u*dt*AdvectionConstant*(*Pressure(i-1,j)-2* *Pressure(i,j)+ *Pressure(i+1,j));
	 
	 *RHS(N) += +0.5*dx*AdvectionConstant* *PressureGradientX(i,j)*ConstX* *PreviousTimeStep(i+1,j);
	 *RHS(N) += -0.5*dx*AdvectionConstant* *PressureGradientX(i,j)*ConstX* *PreviousTimeStep(i-1,j);
	 
	 
	 
	 DiffusionLeft  = DiffusionConstant(u,x,y-0.5*dy,i,j);
	 DiffusionRight = DiffusionConstant(u,x,y+0.5*dy,i,j);
	 
	 
	 *RHS(N) += +0.5*dx*AdvectionConstant* *PressureGradientY(i,j)*ConstY* *PreviousTimeStep(i,j+1);
	 *RHS(N) += -0.5*dx*AdvectionConstant* *PressureGradientY(i,j)*ConstY* *PreviousTimeStep(i,j-1);
	 *RHS(N) += u*dt*AdvectionConstant*(*Pressure(i,j-1)-2* *Pressure(i,j)+ *Pressure(i,j+1));
	 
	 
	 *Coefficients.e(N,N-Yoffset) += (DiffusionLeft)*ConstY;
	 *Coefficients.e(N,N+Yoffset) += (DiffusionRight)*ConstY;
	 *Coefficients.e(N,N) -= ConstY*(DiffusionLeft+DiffusionRight);
	 *Debuger(i,j) = *RHS(N);
	 }
	 
	 */
	
	
	
	
	
}



double VesselStructure::Injection_P(Matrix& CoeffecientX, Matrix& CoeffecientY, Matrix& Variable,int i,int j,double Number)
{

 extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
 extern Matrix BloodVesselRadiusXdir,BloodVesselRadiusYdir;
 extern Matrix TracerX, TracerY;
 extern int Xnodes, Ynodes;

 double pi=3.141592654;
 double Output=0.0;
 double PassByMass=0;
 double Coeffecient=0;
 int i_minus, j_minus, i_plus, j_plus;

 

		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{    
	       if(*CoeffecientX(i, j)<0)
			{			
			Coeffecient +=  pi * pow( *BloodVesselRadiusXdir(i, j), 2);
			//PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j)*(1-*TracerX(i,j)) - *Variable(i, j));			
			}
	
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
	       if(*CoeffecientX(i_plus, j)>0)
			{
			Coeffecient +=  pi * pow( *BloodVesselRadiusXdir(i_plus, j), 2);
			//PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * (*Variable(i_plus, j)*(1-*TracerX(i_plus,j)) - *Variable(i, j));
			}
	
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
		if(*CoeffecientY(i, j)<0)
			{
			Coeffecient += pi * pow( *BloodVesselRadiusYdir(i, j), 2);
			//PassByMass += min_abs( *CoeffecientY(i, j) ) * (*Variable(i, j_minus)*(1-*TracerY(i,j)) - *Variable(i, j));	
			}
		
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
		
		if(*CoeffecientY(i, j_plus)>0)
			{
			Coeffecient +=  pi * pow( *BloodVesselRadiusYdir(i, j_plus), 2);	
			//PassByMass += min_abs( *CoeffecientY(i, j_plus) ) * (*Variable(i, j_plus)*(1-*TracerY(i,j_plus)) - *Variable(i, j));
			//cout<<PassByMass<<"ByMass"<<endl; 
			}
	}

	
	if(Coeffecient>0)
	{
	Output = (1.0/NodeLength)*(Number)/(Coeffecient);
	//cout<<PassByMass<<endl;
	if(Output<0)
	{
	//cout<<PassByMass<<endl;
	Output = 0.0;
	}
	
	}
	else
	{
	Output=0.0;
	}	
	
		
	return Output;
}


double VesselStructure::Injection_DI(Matrix& CoeffecientX, Matrix& CoeffecientY, Matrix& Variable,Matrix& Fraction, int i,int j,double Number)
{
	
	extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
	extern Matrix BloodVesselRadiusXdir,BloodVesselRadiusYdir;
	extern Matrix TracerX, TracerY;
	extern int Xnodes, Ynodes;
	
	double pi=3.141592654;
	double Output=0.0;
	double PassByMass=0;
	double Coeffecient=0;
	int i_minus, j_minus, i_plus, j_plus;
	
	
	
	if(j == Ynodes-1)// boundary condition again
	{
		j_plus = Ynodes-1;
		j_minus=j-1;
	}
	else if( j == 0)
	{
		j_minus =0;
		j_plus=j+1;
	}
	else
	{
		j_plus = j+1;
		j_minus = j-1;
	}
	
	if(i == Xnodes-1)// boundary condition again NOTES
	{
		i_plus = Xnodes-1;
		i_minus= i-1;
	}
	
	else if( i == 0)
	{
		i_minus =0;
		i_plus = i+1;
	}
	else
	{
		i_plus = i+1;
		i_minus = i-1;
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{    
		if(*CoeffecientX(i, j)<0)
		{			
			Coeffecient +=  pi * pow( *BloodVesselRadiusXdir(i, j), 2);
			//PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j)*(1-*TracerX(i,j)) - *Variable(i, j));			
		}
		
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
		if(*CoeffecientX(i_plus, j)>0)
		{
			Coeffecient +=  pi * pow( *BloodVesselRadiusXdir(i_plus, j), 2);
			//PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * (*Variable(i_plus, j)*(1-*TracerX(i_plus,j)) - *Variable(i, j));
		}
		
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
		if(*CoeffecientY(i, j)<0)
		{
			Coeffecient += pi * pow( *BloodVesselRadiusYdir(i, j), 2);
			//PassByMass += min_abs( *CoeffecientY(i, j) ) * (*Variable(i, j_minus)*(1-*TracerY(i,j)) - *Variable(i, j));	
		}
		
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
		
		if(*CoeffecientY(i, j_plus)>0)
		{
			Coeffecient +=  pi * pow( *BloodVesselRadiusYdir(i, j_plus), 2);	
			//PassByMass += min_abs( *CoeffecientY(i, j_plus) ) * (*Variable(i, j_plus)*(1-*TracerY(i,j_plus)) - *Variable(i, j));
			//cout<<PassByMass<<"ByMass"<<endl; 
		}
	}
	
	
	if(Coeffecient>0)
	{
		Output = (1.0/NodeLength)*(Number)/(Coeffecient);
		//cout<<PassByMass<<endl;
		*Fraction(i,j) = Number;
		
		if(Output<0)
		{
			//cout<<PassByMass<<endl;
			Output = 0.0;
		}
		
	}
	else
	{
		Output=0.0;
		*Fraction(i,j) = 0;
	}	
	
	
	return Output;
}


/*
double VesselStructure::Injection_D(Matrix& CoeffecientX, Matrix& CoeffecientY, Matrix& Variable,int i,int j,double Number)
{

 extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
 extern Matrix BloodVesselRadiusXdir,BloodVesselRadiusYdir;
 extern Matrix TracerX, TracerY;
 extern int Xnodes, Ynodes;

 double pi=3.141592654;
 double Output=0.0;
 double PassByMass=0;
 double Coeffecient=0;
 int i_minus, j_minus, i_plus, j_plus;

 

		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{    
	       if(*CoeffecientX(i, j)<0)
			{			
			Coeffecient +=  pi * pow( *BloodVesselRadiusXdir(i, j), 2);
			//PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j)*(1-*TracerX(i,j)) - *Variable(i, j));			
			}
	
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
	       if(*CoeffecientX(i_plus, j)>0)
			{
			Coeffecient +=  pi * pow( *BloodVesselRadiusXdir(i_plus, j), 2);
			//PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * (*Variable(i_plus, j)*(1-*TracerX(i_plus,j)) - *Variable(i, j));
			}
	
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
		if(*CoeffecientY(i, j)<0)
			{
			Coeffecient += pi * pow( *BloodVesselRadiusYdir(i, j), 2);
			//PassByMass += min_abs( *CoeffecientY(i, j) ) * (*Variable(i, j_minus)*(1-*TracerY(i,j)) - *Variable(i, j));	
			}
		
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
		
		if(*CoeffecientY(i, j_plus)>0)
			{
			Coeffecient +=  pi * pow( *BloodVesselRadiusYdir(i, j_plus), 2);	
			//PassByMass += min_abs( *CoeffecientY(i, j_plus) ) * (*Variable(i, j_plus)*(1-*TracerY(i,j_plus)) - *Variable(i, j));
			//cout<<PassByMass<<"ByMass"<<endl; 
			}
	}

	
	if(Coeffecient>0)
	{
	Output = (1.0/NodeLength)*(Number)/(Coeffecient);
	//cout<<PassByMass<<endl;
	if(Output<0)
	{
	//cout<<PassByMass<<endl;
	Output = 0.0;
	}
	
	}
	else
	{
	Output=0.0;
	}	
	
		
	return Output;
}

*/
double VesselStructure::Accumulate(Matrix& CoeffecientX, Matrix& CoeffecientY, Matrix& Variable,int i,int j,double dt)
{

 extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
 extern Matrix BloodVesselRadiusXdir,BloodVesselRadiusYdir;
 extern Matrix TracerX, TracerY;
 extern int Xnodes, Ynodes;

 double pi=3.141592654;
 double Output=0.0;
 double PassByMass=0;
 double Coeffecient=0;
 int i_minus, j_minus, i_plus, j_plus;

 

		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{    
	       if(*CoeffecientX(i, j)>0)
			{			
			Coeffecient +=  2*pi * pow( *BloodVesselRadiusXdir(i, j), 1);
			PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j)* *TracerX(i,j));			
			}
	
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
	       if(*CoeffecientX(i_plus, j)<0)
			{
			Coeffecient +=  2* pi * pow( *BloodVesselRadiusXdir(i_plus, j), 1);
			PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * (*Variable(i_plus, j)* *TracerX(i_plus,j));
			}
	
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
		if(*CoeffecientY(i, j)>0)
			{
			Coeffecient += 2*pi * pow( *BloodVesselRadiusYdir(i, j), 1);
			PassByMass += min_abs( *CoeffecientY(i, j) ) * (*Variable(i, j_minus)* *TracerY(i,j));	
			}
		
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
		
		if(*CoeffecientY(i, j_plus)<0)
			{
			Coeffecient += 2* pi * pow( *BloodVesselRadiusYdir(i, j_plus), 1);	
			PassByMass += min_abs( *CoeffecientY(i, j_plus) ) * (*Variable(i, j_plus)* *TracerY(i,j_plus));
			//cout<<PassByMass<<"ByMass"<<endl; 
			}
	}

	
	if(Coeffecient>0)
	{
	//Output =(1.0e-6)*(dt/NodeLength)*(PassByMass)/(Coeffecient);//this is concentration,1e-6 because mm^2 to micron^2
	Output = dt*(PassByMass);//this is number
	//cout<<PassByMass<<endl;
	if(Output<0)
	{
	//cout<<PassByMass<<endl;
	Output = 0.0;
	}
	
	}
	else
	{
	Output=0.0;
	}	
	
		
	return Output;
}

double VesselStructure::Accumulate_D(Matrix& CoeffecientX, Matrix& CoeffecientY, Matrix& Variable, Matrix& VariableS, int i,int j,double dt)
{

 extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
 extern Matrix BloodVesselRadiusXdir,BloodVesselRadiusYdir;
 extern Matrix Leak;
	extern Matrix Conduct;
 extern int Xnodes, Ynodes;
 //extern Matrix TracerS;
 double pi=3.141592654;
 double Output=0.0;
 extern double Kwall1;
 extern double Kwall2;
 double PassByMass=0;
 double Coeffecient=0;

	double Vt = 1e-15;//m^3
	double CoeffAv= *Conduct(i,j)*1e-15;//unit in m^2

	
	Output = dt/Vt *(Kwall1*CoeffAv* *Variable(i,j) - Kwall2*CoeffAv* *VariableS(i,j)+ *Leak(i,j)*1e-18* *Variable(i,j));
	
	//if(!(abs(Output)<1.0e7))
	//{
	//	cout<<" i "<<i<<" j "<<j<<" CoeffAv "<<CoeffAv<<" Flow "<< *Variable(i,j)<<" Surface "<< *VariableS(i,j);
	//	getchar();
	//}
		
	return Output;
}




double VesselStructure::PTUpdate_Wave(Matrix& CoeffecientX, Matrix& CoeffecientY, Matrix& Variable,int i,int j)
{

 extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
 extern Matrix TracerX, TracerY;
 extern int Xnodes, Ynodes;


 double Output=0.0;
 double PassByMass=0;
 double Coeffecient=0;
 int i_minus, j_minus, i_plus, j_plus;

 

		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{    
	       if(*CoeffecientX(i, j)>0)
			{
			//PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j) +*Variable(i, j) )/2;
			Coeffecient +=  min_abs( *CoeffecientX(i, j));
			PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j))*(1-*TracerX(i,j));
			//Coeffecient += min_abs( *CoeffecientX(i, j));
			}
	
	/* 
			if(*CoeffecientX(i, j)<0)
			{ 
			Coeffecient += min_abs( *CoeffecientX(i, j))* *TracerX(i,j);
			PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j))*(-1-*TracerX(i,j));
			}
			else if(*CoeffecientX(i, j)>0)
			{
			//PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j) +*Variable(i, j) )/2;
			Coeffecient +=  min_abs( *CoeffecientX(i, j))* *TracerX(i,j);
			PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j))*(1-*TracerX(i,j));
			//Coeffecient += min_abs( *CoeffecientX(i, j));
			}
			//cout<<PassByMass<<"ByMass"<<endl; 
	*/
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{	
	
	       if(*CoeffecientX(i_plus, j)<0)
			{
			Coeffecient += min_abs( *CoeffecientX(i_plus, j) );
			PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * (*Variable(i_plus, j))*(1-*TracerX(i_plus,j));
						//cout<<PassByMass<<"ByMass"<<endl; 
			}

	/*	
			if(*CoeffecientX(i_plus, j)>0)
			{
			Coeffecient += min_abs( *CoeffecientX(i_plus, j) )* *TracerX(i_plus, j);
			PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * (*Variable(i_plus, j))*(-1-*TracerX(i_plus,j));

			}
			else if(*CoeffecientX(i_plus, j)<0)
			{
			Coeffecient += min_abs( *CoeffecientX(i_plus, j) )* *TracerX(i_plus, j);
			PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * (*Variable(i_plus, j))*(1-*TracerX(i_plus,j));
						//cout<<PassByMass<<"ByMass"<<endl; 
			}
	*/		
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
		if(*CoeffecientY(i, j)>0)
			{
			Coeffecient += min_abs( *CoeffecientY(i, j));
			PassByMass += min_abs( *CoeffecientY(i, j) ) * (*Variable(i, j_minus))*(1-*TracerY(i,j));	
			}
		/*
				if(*CoeffecientY(i, j)<0)
			{
			Coeffecient += min_abs( *CoeffecientY(i, j))* *TracerY(i,j);
			PassByMass += min_abs( *CoeffecientY(i, j) ) * (*Variable(i, j_minus))*(-1-*TracerY(i,j));
			}
			else if(*CoeffecientY(i, j)>0)
			{
			Coeffecient += min_abs( *CoeffecientY(i, j))* *TracerY(i,j);
			PassByMass += min_abs( *CoeffecientY(i, j) ) * (*Variable(i, j_minus))*(1-*TracerY(i,j));	
			}
			*/
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
		
		if(*CoeffecientY(i, j_plus)<0)
			{
			Coeffecient += min_abs( *CoeffecientY(i, j_plus));
		    PassByMass += min_abs( *CoeffecientY(i, j_plus) ) * (*Variable(i, j_plus))*(1-*TracerY(i,j_plus));
			//cout<<PassByMass<<"ByMass"<<endl; 
			}
		/*
		      if(*CoeffecientY(i, j_plus)>0)
		   {
		   Coeffecient += min_abs( *CoeffecientY(i, j_plus))* *TracerY(i,j_plus);
		   PassByMass += min_abs( *CoeffecientY(i, j_plus) ) * (*Variable(i, j_plus))*(-1-*TracerY(i,j_plus));
		   }
		   else if(*CoeffecientY(i, j_plus)<0)
			{
			Coeffecient += min_abs( *CoeffecientY(i, j_plus))* *TracerY(i,j_plus);
		    PassByMass += min_abs( *CoeffecientY(i, j_plus) ) * (*Variable(i, j_plus))*(1-*TracerY(i,j_plus));
			//cout<<PassByMass<<"ByMass"<<endl; 
			}
			*/
	}

	
	if(Coeffecient>0)
	{
	Output=(PassByMass)/(Coeffecient);
	//cout<<PassByMass<<endl;

	}
	else
	{
	Output=0.0;
	}	
	
		
	return Output;
}
void VesselStructure::Adhere(void)
{
extern Matrix Tracer, TracerS;
extern Matrix TracerX, TracerY;
extern Matrix FlowX, FlowY;
extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
extern Matrix BloodVesselRadiusXdir, BloodVesselRadiusYdir;
extern int Xnodes, Ynodes;
int i_minus, j_minus, i_plus, j_plus, i, j;
double Treatment_t = 2400;
TracerS = zeros(Xnodes, Ynodes);
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
while(pCurrentVesselPoint)
{
i=pCurrentVesselPoint->i;
j=pCurrentVesselPoint->j;

 

		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{     
			if(*FlowX(i,j)>0)
			{
			//*TracerS(i,j) += *TracerX(i,j)*min_abs(*FlowX(i,j))* *Tracer(i_minus,j);
			*TracerS(i,j) += *TracerX(i,j)*min_abs(*FlowX(i,j))* *Tracer(i_minus,j)*Treatment_t;
			}
			
			//*TracerS(i,j) += min_abs(1.0 - *TracerX(i, j)/(*FlowX(i,j)) ) * (*Tracer(i_minus, j)+*Tracer(i, j))/2;
			/*
			if(*TracerX(i, j)<0)
			{ 
			*TracerS(i,j) += min_abs(*FlowX(i,j)- *TracerX(i, j) ) * (*Tracer(i, j));
			}
			else if(*TracerX(i, j)>0)
			{
			//PassByMass += min_abs( *TracerX(i, j) ) * (*Variable(i_minus, j) +*Variable(i, j) )/2;
			*TracerS(i,j) += min_abs( *FlowX(i,j)-*TracerX(i, j) ) * (*Tracer(i_minus, j));
			//Coeffecient += min_abs( *TracerX(i, j));
			}
	
			*/		//cout<<PassByMass<<"ByMass"<<endl; 
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
			if (*FlowX(i_plus,j)<0)
			{
			*TracerS(i,j) += *TracerX(i_plus,j)*min_abs(*FlowX(i_plus,j))* *Tracer(i_plus,j)*Treatment_t;
			}
			//*TracerS(i,j) += min_abs( 1.0-*TracerX(i_plus, j)/(*FlowX(i_plus,j)) ) * (*Tracer(i, j)+*Tracer(i_plus, j))/2;

			/*
			if(*TracerX(i_plus, j)>0)
			{
			*TracerS(i,j) += min_abs( *FlowX(i_plus,j)-*TracerX(i_plus, j) ) * (*Tracer(i, j));


			}
			else if(*TracerX(i_plus, j)<0)
			{
			*TracerS(i,j) += min_abs( *FlowX(i_plus,j)-*TracerX(i_plus, j) ) * (*Tracer(i_plus, j));
						//cout<<PassByMass<<"ByMass"<<endl; 
			}
			*/
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
			if(*FlowY(i,j)>0)
			{
			*TracerS(i,j) += *TracerY(i,j)*min_abs(*FlowY(i,j))* *Tracer(i,j_minus)*Treatment_t;
			}
			
			//*TracerS(i,j) += min_abs(1.0 - *TracerY(i, j)/(*FlowY(i,j)) ) * (*Tracer(i, j)+*Tracer(i, j_minus))/2;

			/*
			if(*TracerY(i, j)<0)
			{
			*TracerS(i,j) += min_abs(*FlowY(i,j)- *TracerY(i, j) ) * (*Tracer(i, j));
			}
			else if(*TracerY(i, j)>0)
			{
			*TracerS(i,j) += min_abs(*FlowY(i,j)-*TracerY(i, j) ) * (*Tracer(i, j_minus));
						//cout<<PassByMass<<"ByMass"<<endl; 
			}
			*/
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
		    if (*FlowX(i,j_plus)<0)
			{
			*TracerS(i,j) += *TracerY(i,j_plus)*min_abs(*FlowY(i,j_plus))* *Tracer(i,j_plus)*Treatment_t;
			}

		  //*TracerS(i,j) += min_abs(1.0 - *TracerY(i, j_plus)/(*FlowY(i,j_plus)) ) * (*Tracer(i, j)+*Tracer(i, j_plus))/2;
		   /*
		    if(*TracerY(i, j_plus)>0)
		   {
		   *TracerS(i,j) += min_abs(*FlowY(i,j_plus)- *TracerY(i, j_plus) ) * (*Tracer(i, j));
		   }
		   else if(*TracerY(i, j_plus)<0)
			{
			*TracerS(i,j) += min_abs(*FlowY(i,j_plus)- *TracerY(i, j_plus) ) * (*Tracer(i, j_plus));
			
			//cout<<PassByMass<<"ByMass"<<endl; 
			}
			*/
	}
	
	

	
pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
}
	

		

}

double VesselStructure::PTUpdate(Matrix& CoeffecientX, Matrix& CoeffecientY, Matrix& Variable,int i,int j)
{

 extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
 extern Matrix ConductX, ConductY;
 extern int Xnodes, Ynodes;

 double Output=0.0;
 double PassByMass=0;
 double Coeffecient=0;
 int i_minus, j_minus, i_plus, j_plus;

 

		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{    
		if(*CoeffecientX(i, j)>0)
		{
			//PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j) +*Variable(i, j) )/2;
			Coeffecient +=  min_abs( *CoeffecientX(i, j));
			PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j));
			//Coeffecient += min_abs( *CoeffecientX(i, j));
		}
		
		/* 
		 if(*CoeffecientX(i, j)<0)
		 { 
		 Coeffecient += min_abs( *CoeffecientX(i, j))* *TracerX(i,j);
		 PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j))*(-1-*TracerX(i,j));
		 }
		 else if(*CoeffecientX(i, j)>0)
		 {
		 //PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j) +*Variable(i, j) )/2;
		 Coeffecient +=  min_abs( *CoeffecientX(i, j))* *TracerX(i,j);
		 PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j))*(1-*TracerX(i,j));
		 //Coeffecient += min_abs( *CoeffecientX(i, j));
		 }
		 //cout<<PassByMass<<"ByMass"<<endl; 
		 */
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{	
		
		if(*CoeffecientX(i_plus, j)<0)
		{
			Coeffecient += min_abs( *CoeffecientX(i_plus, j) );
			PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * (*Variable(i_plus, j));
			//cout<<PassByMass<<"ByMass"<<endl; 
		}
		
		/*	
		 if(*CoeffecientX(i_plus, j)>0)
		 {
		 Coeffecient += min_abs( *CoeffecientX(i_plus, j) )* *TracerX(i_plus, j);
		 PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * (*Variable(i_plus, j))*(-1-*TracerX(i_plus,j));
		 
		 }
		 else if(*CoeffecientX(i_plus, j)<0)
		 {
		 Coeffecient += min_abs( *CoeffecientX(i_plus, j) )* *TracerX(i_plus, j);
		 PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * (*Variable(i_plus, j))*(1-*TracerX(i_plus,j));
		 //cout<<PassByMass<<"ByMass"<<endl; 
		 }
		 */		
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
		if(*CoeffecientY(i, j)>0)
		{
			Coeffecient += min_abs( *CoeffecientY(i, j));
			PassByMass += min_abs( *CoeffecientY(i, j) ) * (*Variable(i, j_minus));	
		}
		/*
		 if(*CoeffecientY(i, j)<0)
		 {
		 Coeffecient += min_abs( *CoeffecientY(i, j))* *TracerY(i,j);
		 PassByMass += min_abs( *CoeffecientY(i, j) ) * (*Variable(i, j_minus))*(-1-*TracerY(i,j));
		 }
		 else if(*CoeffecientY(i, j)>0)
		 {
		 Coeffecient += min_abs( *CoeffecientY(i, j))* *TracerY(i,j);
		 PassByMass += min_abs( *CoeffecientY(i, j) ) * (*Variable(i, j_minus))*(1-*TracerY(i,j));	
		 }
		 */
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
		
		if(*CoeffecientY(i, j_plus)<0)
		{
			Coeffecient += min_abs( *CoeffecientY(i, j_plus));
		    PassByMass += min_abs( *CoeffecientY(i, j_plus) ) * (*Variable(i, j_plus));
			//cout<<PassByMass<<"ByMass"<<endl; 
		}
		/*
		 if(*CoeffecientY(i, j_plus)>0)
		 {
		 Coeffecient += min_abs( *CoeffecientY(i, j_plus))* *TracerY(i,j_plus);
		 PassByMass += min_abs( *CoeffecientY(i, j_plus) ) * (*Variable(i, j_plus))*(-1-*TracerY(i,j_plus));
		 }
		 else if(*CoeffecientY(i, j_plus)<0)
		 {
		 Coeffecient += min_abs( *CoeffecientY(i, j_plus))* *TracerY(i,j_plus);
		 PassByMass += min_abs( *CoeffecientY(i, j_plus) ) * (*Variable(i, j_plus))*(1-*TracerY(i,j_plus));
		 //cout<<PassByMass<<"ByMass"<<endl; 
		 }
		 */
	}
	
	
	if(Coeffecient>0)
	{
	Output=1.85*(PassByMass)/(Coeffecient) - 0.85* *Variable(i, j);
	}
	else
	{
	Output = 0;
	}
	 
	
		
	return Output;
}



void VesselStructure::SteadyStateHematocrit()
{
//cout <<"function UpdateHematocrit running"<<endl; 

 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodNodePressure;
 extern Matrix BloodVesselPressureX;
 extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
 extern Matrix FlowX, FlowY;
 extern Matrix WssX, WssY;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix ConductX, ConductY; //conductivity
 extern int Xnodes, Ynodes;
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
	extern Matrix BloodVesselIndicator;
	extern bool VesselBroke;
    double FlowMin=1.0e-30;
	double TimeStepMax= AngioDeltaT;
	int MinTime_i,MinTime_j;
	double pi=3.141592654;
	double TimeStep = AngioDeltaT;
	double TimeFactor;
	TimeFactor = 1.0;
	double MaxHt = RetrieveDoubleParameter( ParameterFile , "MaxHt");
	double AverageHt = RetrieveDoubleParameter( ParameterFile , "AverageHt");
	Matrix Flow;
	Matrix PassByMass;
	Flow = zeros( Xnodes, Ynodes);
	PassByMass = zeros( Xnodes, Ynodes);
	int i_minus, j_minus, i_plus, j_plus;
	int i, j;
	double Tolerance=0.00001;
	int Iterations=0;
	double MaxError=2*Tolerance;
	int MaxIterations=5000000;
	
	
	for(i = 0; i< Xnodes; i++)
	{
		for(j = 0; j< Ynodes; j++)
		{
		if(*BloodVesselIndicator(i,j)==0)
		{
			*HaematocritLevel(i,j) = 0;	
		}
			
			
		}
	}
	
	
	while(pCurrentVesselPoint)
	{
		 i = pCurrentVesselPoint->i;
		 j = pCurrentVesselPoint->j;
		
		//cout<<"here after"<<endl;
				//g<<"here before"<<endl;
		
		//cout<<*Flow(i,j)<<endl;
		//cout<<min_abs( *FlowX(i, j) )<<endl;
		
	//	cout<<*FlowX(i,j)<<endl;
	//	cout<<*FlowY(i,j)<<endl;
		if(min_abs( *FlowX(i, j) ) > FlowMin)
		{
			TimeStep = pi * pow( *BloodVesselRadiusXdir(i, j), 2) * NodeLength / min_abs( *FlowX(i, j) );
			//cout<<"TimeStep"<<TimeStep<<endl;
			if (TimeStep < TimeStepMax)
			{
				TimeStepMax = TimeStep;
				//cout<<"TimeStepMax"<<TimeStepMax<<endl;
			}
			
		}
		
		if(min_abs( *FlowY(i, j) ) > FlowMin)
		{
			TimeStep = pi * pow( *BloodVesselRadiusYdir(i, j), 2) * NodeLength / min_abs( *FlowY(i, j) );
			if (TimeStep < TimeStepMax)
			{
				TimeStepMax = TimeStep;
			}

		}
	  
	  pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
	}
	
	//AngioDeltaT = 0.05;//TimeStepMax / TimeFactor;
	cout<<"AngioDeltaT"<<AngioDeltaT<<endl;
   //if(VesselBroke)//add on Sep 22,2009
  // {
  // HaematocritLevel=zeros(Xnodes, Ynodes);//add on Sep 13, 2009
  // VesselBroke=0;
  // }
while(MaxError>Tolerance&&Iterations<MaxIterations)
 {
//relocalize the point
 pCurrentVesselPoint = pFirstVesselPoint;
 MaxError=0;
 double Error=0.0;
  //sweep along the chain
 while(pCurrentVesselPoint)
 {
 i=pCurrentVesselPoint->i;
 j=pCurrentVesselPoint->j;
 if(pCurrentVesselPoint->AmIArtery||pCurrentVesselPoint->AmIVenous)
	{
		//*HemX(i, j) =0.45;//+= min_abs(*FlowX(i, j) * AngioDeltaT 
		            //        / ( pi * pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength )
					//	    * ( TimeFactor * AverageHt - TimeFactor * *HemX(i, j) ) / MaxHt) ;
		*HaematocritLevel(i, j) =0.45;//+= min_abs(*FlowY(i, j) * AngioDeltaT 
		              //      / ( pi * pow( *BloodVesselRadiusYdir(i, j), 2 ) * NodeLength )
						//    * ( TimeFactor * AverageHt - TimeFactor * *HemY(i, j) ) / MaxHt) ;	
							//cout<<*FlowX(i, j)<<"  "<<AngioDeltaT<<"  "<<pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength<<"  "<<( TimeFactor * AverageHt - TimeFactor * *HemX(i, j) )<<endl;
							//cout<<*HemX(i, j)<<"   "<<*HemY(i, j)<<endl;
	//cout<<"Venous or artery"<<i<<" ,"<<j<<"   ";
	//getchar();
	
	}
 // else if(i==0)
 // {
 //    *HaematocritLevel(i, j) =0.45;
  //}
  else
  {
   // cout<<"PTU ING"<<endl;
  double NewValue = PTUpdate(FlowX, FlowY, HaematocritLevel, i, j);
   // cout<<"PTU END"<<endl;
    Error = min_abs( NewValue - *HaematocritLevel(i,j) );
   if( Error > MaxError )
	{ MaxError = Error;
	//cout<<"at"<<i<<" ,"<<j<<"   "<<NewValue<<"   ";
	 }
	*HaematocritLevel(i,j) = NewValue;
 
//cout<<"NewValue:  "<<NewValue<<endl;


   
   }	 

 
  
   pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//Move on
  }
 //REVERSE THE DIRECTION
 //cout<<endl;

 pCurrentVesselPoint = pLastVesselPoint;
 
 while(pCurrentVesselPoint)
 {
 i=pCurrentVesselPoint->i;
 j=pCurrentVesselPoint->j;
 if(pCurrentVesselPoint->AmIArtery||pCurrentVesselPoint->AmIVenous)
	{
		//*HemX(i, j) =0.45;//+= min_abs(*FlowX(i, j) * AngioDeltaT 
		            //        / ( pi * pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength )
					//	    * ( TimeFactor * AverageHt - TimeFactor * *HemX(i, j) ) / MaxHt) ;
		*HaematocritLevel(i, j) =0.45;//+= min_abs(*FlowY(i, j) * AngioDeltaT 
		              //      / ( pi * pow( *BloodVesselRadiusYdir(i, j), 2 ) * NodeLength )
						//    * ( TimeFactor * AverageHt - TimeFactor * *HemY(i, j) ) / MaxHt) ;	
							//cout<<*FlowX(i, j)<<"  "<<AngioDeltaT<<"  "<<pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength<<"  "<<( TimeFactor * AverageHt - TimeFactor * *HemX(i, j) )<<endl;
							//cout<<*HemX(i, j)<<"   "<<*HemY(i, j)<<endl;
	}
//	else if(i==0)
 // {
 //    *HaematocritLevel(i, j) =0.45;
  //}
 else
   {
 double NewValue = PTUpdate(FlowX, FlowY, HaematocritLevel, i, j);
 
  Error = min_abs( NewValue - *HaematocritLevel(i,j) );
 if( Error > MaxError )
	{ MaxError = Error;
	//cout<<"at"<<i<<" ,"<<j<<"   "<<NewValue<<"   ";
	}
	*HaematocritLevel(i,j) = NewValue; 
	
	
  
   }
   
 pCurrentVesselPoint = pCurrentVesselPoint->pPreviousVesselPoint;//Move on
 }

 Iterations++;


 Iterations++;

  if( Iterations % 1000 == 0 )
  { 
	cout << Iterations << " : " << MaxError << endl; }  

 }
	
	
	
	for(i = 0; i< Xnodes; i++)
	{
		for(j = 0; j< Ynodes; j++)
		{
			if(*HaematocritLevel(i,j)>0.45)
			{
				*HaematocritLevel(i,j) = 0.45;	
			}
			
			
		}
	}
 cout << "summary:: iterations: " << Iterations << endl
      << "          error     : " << MaxError << endl << endl; 
//AngioDeltaT=0.05;	  
  
 cout <<"function UpdateHematocrit finished"<<endl;
 

 }
 
 void GetMaxTracer()
 {
 extern double TracerMax;
 extern int Xnodes,Ynodes;
 extern Matrix PreExist, BloodVesselIndicator, TracerS;
 TracerMax = 0;
 for(int i=0; i<Xnodes; i++)
 {
	for(int j=0; j<Ynodes; j++)
	{
	if(*PreExist(i,j)==0&&*BloodVesselIndicator(i,j)>0)
	{
	if(*TracerS(i,j)>TracerMax)
	{
	TracerMax = *TracerS(i,j);
	}
	}
	}
 }
 
 for(int i=0; i<Xnodes; i++)
 {
	for(int j=0; j<Ynodes; j++)
	{
	
	
	if(*PreExist(i,j)==0&&*BloodVesselIndicator(i,j)>0)
	{	
	*TracerS(i,j) = *TracerS(i,j)/TracerMax;
	}
	else
	{
	*TracerS(i,j) = 0;
	}
	
	
	}
 }
 
 
 
 }
 
  void VesselStructure::Transportation_P()
{
//cout <<"function UpdateHematocrit running"<<endl; 
 extern char SaveDirectory[1025];
 char temp_string[1024];
 
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodNodePressure;
 extern Matrix BloodVesselPressureX;
 extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
 extern Matrix FlowX, FlowY;
 extern Matrix WssX, WssY;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix ConductX, ConductY; //conductivity
 extern int Xnodes, Ynodes;
 extern Matrix Tracer,TracerS;
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
    double Treatment = 40;
	extern bool VesselBroke;
    double FlowMin=1.0e-30;
	double TimeStepMax= AngioDeltaT*36*60*60;
	int MinTime_i,MinTime_j;
	double pi=3.141592654;
	double TimeStep = AngioDeltaT;
	double TimeFactor;
	TimeFactor = 1.0;
	double MaxHt = RetrieveDoubleParameter( ParameterFile , "MaxHt");
	double AverageHt = RetrieveDoubleParameter( ParameterFile , "AverageHt");
	Matrix Flow;
	Matrix PassByMass;
	Matrix Hem;
	Flow = zeros( Xnodes, Ynodes);
	PassByMass = zeros( Xnodes, Ynodes);
	Hem = zeros(Xnodes, Ynodes);
	Tracer = zeros(Xnodes, Ynodes);
	TracerS = zeros(Xnodes, Ynodes);
	extern double t;
	//Hem = zeros(Xnodes, Ynodes);
	
	int i_minus, j_minus, i_plus, j_plus;
	int i, j;
	double Tolerance=10;
	int Iterations=0;
	double MaxError=2*Tolerance;
	double Error = 0;
	int MaxIterations=5000000;
	
	while(pCurrentVesselPoint)
	{
		 i = pCurrentVesselPoint->i;
		 j = pCurrentVesselPoint->j;
		
		if(min_abs( *FlowX(i, j) ) > FlowMin)
		{
			TimeStep = pi * pow( *BloodVesselRadiusXdir(i, j), 2) * NodeLength / min_abs( *FlowX(i, j) );
			//cout<<"TimeStep"<<TimeStep<<endl;
			if (TimeStep < TimeStepMax)
			{
				TimeStepMax = TimeStep;
				//cout<<"TimeStepMax"<<TimeStepMax<<endl;
			}
			
		}
		
		if(min_abs( *FlowY(i, j) ) > FlowMin)
		{
			TimeStep = pi * pow( *BloodVesselRadiusYdir(i, j), 2) * NodeLength / min_abs( *FlowY(i, j) );
			if (TimeStep < TimeStepMax)
			{
				TimeStepMax = TimeStep;
			}

		}
	  
	  pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
	}
	
	//AngioDeltaT = 0.05;//TimeStepMax / TimeFactor;
	cout<<"TimeStep:   "<<TimeStepMax<<endl;
 
//initialization 


//MaxIterations = int(floor(Treatment*60/TimeStepMax));
MaxIterations = int(floor(40*60/TimeStepMax));
cout<<"MaxIterations   "<<MaxIterations<<endl;
//interate
while(Iterations<MaxIterations&&MaxError>Tolerance)
{
//relocalize the point
 pCurrentVesselPoint = pFirstVesselPoint;

  //sweep along the chain
 while(pCurrentVesselPoint)
 {
 i=pCurrentVesselPoint->i;
 j=pCurrentVesselPoint->j;
 
   //  cout<<"PTU ING"<<endl;
	
 //if(pCurrentVesselPoint->AmIArtery||pCurrentVesselPoint->AmIVenous)
	if(pCurrentVesselPoint->AmIArtery)
	{
		
		if(Iterations<1)
		{
		*Tracer(i,j) = Injection_P(FlowX, FlowY, Hem, i, j, 0.0625);//+= min_abs(*FlowY(i, j) * AngioDeltaT 
		}
		else
		{
		*Tracer(i,j) = 0;
		
		}	
	}
 else
    {
		*Tracer(i,j) = Transport_P(FlowX, FlowY, Hem, i, j, TimeStepMax);
		*TracerS(i,j) += Accumulate(FlowX, FlowY, Hem, i, j, TimeStepMax);
		
	}	
 	      	
   pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//Move on
 }
 Iterations++; 
// cout<<"PTU ING"<<endl;

  pCurrentVesselPoint = pFirstVesselPoint;
MaxError = 0;
  //sweep along the chain


 while(pCurrentVesselPoint)
 {
 i=pCurrentVesselPoint->i;
 j=pCurrentVesselPoint->j;
 Error = min_abs(*Hem(i,j)-*Tracer(i,j));
  if(MaxError<Error)
  {
  MaxError = Error;
  }
    // cout<<"PTU ING"<<endl;
  
 	*Hem(i,j) = *Tracer(i,j);
   pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//Move on
 }
 
 
  if( Iterations % 1000 == 0 )
  { cout << Iterations << " : "  <<MaxIterations<< endl; }  

 /*
	sprintf( temp_string, "%s/ParticleFlow_%d.txt", SaveDirectory,Iterations);
     ofstream ParticleFlow( temp_string, ios::out );

 for( i=0;i<Xnodes;i++)
 {
	for( j=0;j<Ynodes;j++)
	{
		 ParticleFlow<<*Tracer(i,j)<<"\t";
	 
	}
	
	 ParticleFlow<<endl;
 }
 
 ParticleFlow.close();	

 sprintf( temp_string, "%s/ParticleSurface_%d.txt", SaveDirectory,Iterations);
     ofstream ParticleSurface( temp_string, ios::out );

 for( i=0;i<Xnodes;i++)
 {
	for( j=0;j<Ynodes;j++)
	{
		 ParticleSurface<<*TracerS(i,j)<<"\t";
	 
	}
	
	ParticleSurface<<endl;
 }
 
ParticleSurface.close();	

*/

}
	
 

sprintf( temp_string, "%s/record.txt", SaveDirectory);
     ofstream record( temp_string, ios::out );


		record<<TimeStepMax<<"\t";
		 record<<Iterations<<"\t";
	 record<<Iterations*TimeStepMax<<"\t";
	
	record<<endl;

 
record.close();	

  
 cout <<"function TimeTransport finished"<<endl;
 }

void VesselStructure::UpdateViscosity()
{
 //cout <<"function UpdateViscosity running"<<endl; 
 //extern Matrix DirectionX, DirectionY;
 extern Matrix FlowX, FlowY;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
 //extern Matrix HemX, HemY;
 extern Matrix HaematocritLevel;
 extern Matrix WssX, WssY;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix ConductX, ConductY; //conductivity
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
	extern Matrix CollapseClock;
 double mu_plasma = RetrieveDoubleParameter( ParameterFile, "mu_plasma");
 double AverageHt = RetrieveDoubleParameter( ParameterFile , "AverageHt");
 double Temp1, Temp2, TempHt, Temp_045;
 extern int Xnodes, Ynodes;
  ofstream myfile15;
  myfile15.open ("debugVis.txt");




 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 int i, j;
 while(pCurrentVesselPoint)
 {
	i = pCurrentVesselPoint->i;
	j = pCurrentVesselPoint->j;
	//if(!(pCurrentVesselPoint->AmIArtery))
	
	
	 
	 {
	
	
		//this may change, how do they come?
		if(i!=0&&(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5))
		{
		
		if( (*HaematocritLevel(i-1, j)+*HaematocritLevel(i, j))/2 < 0.0001 ||*CollapseClock(i,j)>0)
	{
		*VisX(i, j) = mu_plasma;
		//cout<<*VisX(i,j);
	}
	
	else
		{
		Temp1 = 1.0 / ( 1 + pow( 10.0, -11.0 ) * pow( 2.0 * *BloodVesselRadiusXdir(i, j), 12.0 ) );
		Temp2 = (0.8 + exp( -0.15 * *BloodVesselRadiusXdir(i, j) ) ) * ( -1.0 + Temp1 ) + Temp1;
		TempHt = ( pow( (1.0 - (*HaematocritLevel(i-1, j)+*HaematocritLevel(i, j))/2 ), Temp2 ) - 1.0 ) / ( pow( ( 1.0 - AverageHt ), Temp2 ) - 1.0 ); 
		Temp_045 = 6 * exp( -0.17 * *BloodVesselRadiusXdir(i, j) )
		           + 3.2 - 2.44 * exp( -0.06 * pow(2 * *BloodVesselRadiusXdir(i, j), 0.645) );  
		*VisX(i, j) = mu_plasma * pow( ( 2 * *BloodVesselRadiusXdir(i, j)*1e6 / ( 2 *  *BloodVesselRadiusXdir(i, j)*1e6 - 1.1 ) ), 2 )
					 *( 1 + ( Temp_045 - 1 ) * TempHt * pow( ( 2 * *BloodVesselRadiusXdir(i, j)*1e6 / ( 2 *  *BloodVesselRadiusXdir(i, j)*1e6 - 1.1 ) ), 2 ) );
		
		}
		//myfile15 <<"VisX "<<*VisX(i,j)<<" at  "<<i<<", "<<j<<endl;
		}			 
		
	
	
	if(j!=0&&(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5))
		{
		
		if( (*HaematocritLevel(i, j-1)+*HaematocritLevel(i, j))/2 < 0.0001 ||*CollapseClock(i,j)>0)
	{
		*VisY(i, j) = mu_plasma;
	}
	else
	   {
		Temp1 = 1.0 / ( 1 + pow( 10.0, -11.0 ) * pow( 2.0 * *BloodVesselRadiusYdir(i, j), 12.0 ) );
		Temp2 = (0.8 + exp( -0.15 * *BloodVesselRadiusYdir(i, j) ) ) * ( -1.0 + Temp1 ) + Temp1;
		TempHt = ( pow( (1.0 - (*HaematocritLevel(i, j-1)+*HaematocritLevel(i, j))/2 ), Temp2 ) - 1.0 ) / ( pow( ( 1.0 - AverageHt ), Temp2 ) - 1.0 ); 
		Temp_045 = 6 * exp( -0.17 * *BloodVesselRadiusYdir(i, j) )
		           + 3.2 - 2.44 * exp( -0.06 * pow(2 * *BloodVesselRadiusYdir(i, j), 0.645) );  
		*VisY(i, j) = mu_plasma * pow( ( 2 * *BloodVesselRadiusYdir(i, j)*1e6 / ( 2 *  *BloodVesselRadiusYdir(i, j)*1e6 - 1.1 ) ), 2 )
					 *( 1 + ( Temp_045 - 1 ) * TempHt * pow( ( 2 * *BloodVesselRadiusYdir(i, j)*1e6 / ( 2 *  *BloodVesselRadiusYdir(i, j) *1e6- 1.1 ) ), 2 ) );
		
		}
		//myfile15 <<"VisY "<<*VisY(i,j)<<" at  "<<i<<", "<<j<<endl;
		}
	
	 }

	 
	pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;

 }
 
 
	cout <<"function UpdateViscosity finished"<<endl; 
myfile15.close();

}



void VesselStructure::Transportation_DI(double time)
{
	//cout <<"function UpdateHematocrit running"<<endl; 
	extern char SaveDirectory[1025];
	char temp_string[1024];
	int Jump = (int) RetrieveDoubleParameter( ParameterFile , "Jump");
	extern Matrix BloodVesselRadiusXdir;
	extern Matrix BloodVesselRadiusYdir;
	extern Matrix BloodNodePressure;
	extern Matrix BloodVesselPressureX;
	extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
	extern Matrix FlowX, FlowY;
	
	extern Matrix WssX, WssY;
	extern Matrix HaematocritLevel;
	extern Matrix VisX, VisY; //viscosity
	extern Matrix ConductX, ConductY; //conductivity
	extern int Xnodes, Ynodes;
	extern Matrix Tracer,TracerS;
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
    double Treatment = 40;
	extern bool VesselBroke;
    double FlowMin=1.0e-30;
	double TimeStepMax= AngioDeltaT*36*60*60;
	int MinTime_i,MinTime_j;
	double pi=3.141592654;
	double TimeStep = AngioDeltaT;
	double TimeFactor;
	TimeFactor = 1.0;
	extern double Kwall1;
	extern double Kwall2;
	Kwall1 = RetrieveDoubleParameter( ParameterFile , "Kwall1");
	Kwall2 = RetrieveDoubleParameter( ParameterFile , "Kwall2");
	double MaxHt = RetrieveDoubleParameter( ParameterFile , "MaxHt");
	double AverageHt = RetrieveDoubleParameter( ParameterFile , "AverageHt");
	Matrix Flow;
	Matrix PassByMass;
	Matrix Hem;
	Matrix HemS;
	Matrix HemD;
	Flow = zeros( Xnodes, Ynodes);
	PassByMass = zeros( Xnodes, Ynodes);
	
	Hem = Tracer;
	HemD = TracerS;
	
	HemS = zeros(Xnodes, Ynodes);
	
	Matrix Fraction;
	Fraction = zeros(Xnodes,Ynodes);
	
	//Tracer = zeros(Xnodes, Ynodes);
	//TracerS = zeros(Xnodes, Ynodes);
	extern double t;
	extern double D_DRG;
	
    Matrix DiffusionConstant;
	DiffusionConstant = zeros(Xnodes,Ynodes);
	//Hem = zeros(Xnodes, Ynodes);
	double NewValue;
	int i_minus, j_minus, i_plus, j_plus;
	int i, j;
	for(i=0;i<Xnodes; i++)
	{
		for(j=0;j<Ynodes; j++)
		{
			*DiffusionConstant(i,j) = D_DRG;
			//cout<<*DiffusionConstant(i,j);
		}
	}
	double Tolerance=1.0e-18;
	int Iterations=0;
	double MaxError=2*Tolerance;
	double Error = 0;
	int MaxIterations=5000000;
	
	double ToleranceI=1.0e-18;
	int IterationsI = 0;
	double MaxErrorI = 2*ToleranceI;
	double ErrorI = 0;
	int MaxIterationsI=5000;

	
	
	
	while(pCurrentVesselPoint)
	{
		i = pCurrentVesselPoint->i;
		j = pCurrentVesselPoint->j;
		
		if(min_abs( *FlowX(i, j) ) > FlowMin)
		{
			TimeStep = pi * pow( *BloodVesselRadiusXdir(i, j), 2) * NodeLength / min_abs( *FlowX(i, j) );
			//cout<<"TimeStep"<<TimeStep<<endl;
			if (TimeStep < TimeStepMax)
			{
				TimeStepMax = TimeStep;
				//cout<<"TimeStepMax"<<TimeStepMax<<endl;
			}
			
		}
		
		if(min_abs( *FlowY(i, j) ) > FlowMin)
		{
			TimeStep = pi * pow( *BloodVesselRadiusYdir(i, j), 2) * NodeLength / min_abs( *FlowY(i, j) );
			if (TimeStep < TimeStepMax)
			{
				TimeStepMax = TimeStep;
			}
			
		}
		
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
	}
	
	
	
	//AngioDeltaT = 0.05;//TimeStepMax / TimeFactor;
	cout<<"TimeStep:   "<<TimeStepMax<<endl;
	
	//initialization 
	
	
	//MaxIterations = int(floor(Treatment*60/TimeStepMax));
	MaxIterations = int(floor(6480.0/TimeStepMax));//0.05*36*60*60
	cout<<"MaxIterations   "<<MaxIterations<<endl;
	//interate
	//while(Iterations<MaxIterations&&MaxError>Tolerance)
	while(Iterations<MaxIterations)
	{
		cout << Iterations << " oooo: "  <<MaxIterations<< endl; 
		Iterations++; 
		
		//relocalize the point
		pCurrentVesselPoint = pFirstVesselPoint;
		
		//sweep along the chain
		
		while(pCurrentVesselPoint)
		{
			i=pCurrentVesselPoint->i;
			j=pCurrentVesselPoint->j;
			
			  //cout<<"PTU ING in ";
			
			//if(pCurrentVesselPoint->AmIArtery||pCurrentVesselPoint->AmIVenous)
			if(pCurrentVesselPoint->AmIArtery)
			{
				
				if(Iterations<2 && time>=8.00)
				{
					*Tracer(i,j) = Injection_DI(FlowX, FlowY, Hem, Fraction, i, j, 0.0625);//+= min_abs(*FlowY(i, j) * AngioDeltaT 
					//*Tracer(i,j) *= 1e-15;
				    *HemS(i,j) = 0;
				}
				else
				{
					*Tracer(i,j) = 0;
					*Fraction(i,j)= 0;
					*HemS(i,j) = 0;
					
					
				}	
			}
			else
			{
				
				
				*Tracer(i,j) = Transport_DI(FlowX, FlowY, Hem, HemD, Fraction,i, j, TimeStepMax);
				*HemS(i,j) = Accumulate_D(FlowX, FlowY, Hem, HemD, i, j, TimeStepMax);
				//if(j==200)
				//{
				//cout << *Tracer(i,j)<<" 1 ";	
				//}
				
				
				//*TracerS(i,j) = Accumulate_D(FlowX, FlowY,Hem, TracerS, i, j, TimeStepMax);
				
				
				
				
			}	
 	      	
			pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//Move on
		}
		
		
		
		
		 //cout<<"PTU ING"<<endl;
		
		pCurrentVesselPoint = pFirstVesselPoint;
		MaxError = 0;
		//sweep along the chain
		
		
		while(pCurrentVesselPoint)
		{
			i=pCurrentVesselPoint->i;
			j=pCurrentVesselPoint->j;
			Error = min_abs(*Hem(i,j)-*Tracer(i,j));
			if(MaxError<Error)
			{
				MaxError = Error;
			}
			
			
			*Hem(i,j) = *Tracer(i,j);
			
			pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//Move on
		}
		
		 //cout<<"PTU ING"<<endl;
		
		if( Iterations % Jump == 0 )
		{ 
			
			
				
			IterationsI = 0;
			MaxErrorI = 2*ToleranceI;
			ErrorI = 0;
			double ds = Jump * TimeStepMax;
				
			while(MaxErrorI>ToleranceI&&IterationsI<MaxIterationsI)
			{
				
				
				MaxErrorI = 0;
			
			for( i=0 ; i <= Xnodes-1 ; i++ )
			{
				j = 0;
				NewValue = ImplicitConvectUpdateBoundaryPoint( DiffusionConstant,  HemS, HemD, DRGsinkFunctionBreak, i, j, ds);
				
                

				
				
				ErrorI = min_abs( NewValue - *TracerS(i,j) );
				if( ErrorI > MaxErrorI )
				{ MaxErrorI = ErrorI; }
				*TracerS(i, j) = NewValue;
				
				j = Ynodes-1;
				NewValue = ImplicitConvectUpdateBoundaryPoint( DiffusionConstant,  HemS, HemD,DRGsinkFunctionBreak, i, j,ds);
				ErrorI = min_abs( NewValue - *TracerS(i,j) );
				if( ErrorI > MaxErrorI )
				{ MaxErrorI = ErrorI; }
				*TracerS(i, j) = NewValue;
				
			}
			
			for( j=1 ; j < Ynodes-1 ; j++ )
			{
				i = 0;
				NewValue = ImplicitConvectUpdateBoundaryPoint( DiffusionConstant,  HemS, HemD,DRGsinkFunctionBreak, i, j,ds);
				ErrorI = min_abs( NewValue - *TracerS(i,j) );
				if( ErrorI > MaxErrorI )
				{ MaxErrorI = ErrorI; }
				*TracerS(i, j) = NewValue;
				
				i = Xnodes-1;
				NewValue = ImplicitConvectUpdateBoundaryPoint( DiffusionConstant,  HemS, HemD,DRGsinkFunctionBreak, i, j,ds);
				ErrorI = min_abs( NewValue - *TracerS(i,j) );
				if( ErrorI > MaxErrorI )
				{ MaxErrorI = ErrorI; }
				*TracerS(i, j) = NewValue;
			}
				
			
			//unboundary
			if( IterationsI % 4 == 0 )
			{
				for( j=1 ; j < Ynodes-1 ; j++ )
				{
					for( i=1 ; i < Xnodes-1 ; i++ )
					{
						//cout<<IterationsI;
						//getchar();
						NewValue = ImplicitConvectUpdate( DiffusionConstant, HemS, HemD, DRGsinkFunctionBreak, i, j,ds);
						//cout<<IterationsI;
						//getchar();
						ErrorI = min_abs( NewValue - *TracerS(i, j) );
						if( ErrorI > MaxErrorI )
						{ 
							MaxErrorI = ErrorI;
							
							
						}
						*TracerS(i, j) = NewValue;
						
					}
				}
			}
			
			if( IterationsI % 4 == 1 )
			{
				for( j=Ynodes-2 ; j > 0 ; j-- )
				{
					for( i=1 ; i < Xnodes-1 ; i++ )
					{
						NewValue = ImplicitConvectUpdate( DiffusionConstant, HemS, HemD, DRGsinkFunctionBreak,  i, j,ds);
						ErrorI = min_abs( NewValue - *TracerS(i, j) );
						if( ErrorI > MaxErrorI )
						{ 
							MaxErrorI = ErrorI;
							
							
						}
						*TracerS(i, j) = NewValue;
						
						
					}
				}
			}
			
			if( IterationsI % 4 == 2 )
			{
				for( j=Ynodes-2 ; j > 0 ; j-- )
				{
					for( i=Xnodes-2 ; i > 0 ; i-- )
					{
						NewValue = ImplicitConvectUpdate( DiffusionConstant, HemS, HemD, DRGsinkFunctionBreak,  i, j,ds);
						ErrorI = min_abs( NewValue - *TracerS(i, j) );
						if( ErrorI > MaxErrorI )
						{ 
							MaxErrorI = ErrorI;
							
							
						}
						*TracerS(i, j) = NewValue;
						
					}
				}
			}
			
			if( IterationsI % 4 == 3 )
			{
				for( j=1 ; j < Ynodes-1 ; j++ )
				{
					for( i=Xnodes-2 ; i > 0 ; i-- )
					{
						NewValue = ImplicitConvectUpdate( DiffusionConstant, HemS, HemD, DRGsinkFunctionBreak,  i, j,ds);
						ErrorI = min_abs( NewValue - *TracerS(i, j) );
						if( ErrorI > MaxErrorI )
						{ 
							MaxErrorI = ErrorI;
							
							
						}
						*TracerS(i, j) = NewValue;
						
						
					}
				}
			}
			
			
				
				IterationsI++;
				//cout<<IterationsI;
				
				if( IterationsI % 1000 == 0 )
					
				{
					cout << IterationsI << " : "  <<MaxIterationsI<< endl; 
					cout<<MaxErrorI<<endl;
				}
				
			
			}
			
		}
			
			
			HemD = TracerS;
			
			
				//cout<<"PTU ING"<<endl;
				if(Iterations %100 ==0 && time>= 8.00&&time<8.05)
				{
				
				sprintf( temp_string, "%s/ParticleSource_%d.txt", SaveDirectory,Iterations);
				ofstream ParticleSurface( temp_string, ios::out );
				
				for( i=0;i<Xnodes;i++)
				{
					for( j=0;j<Ynodes;j++)
					{
						ParticleSurface<<*HemS(i,j)<<"\t";
						
					}
					
					ParticleSurface<<endl;
				}
				
				
				
				
				
			
			
			
			
			sprintf( temp_string, "%s/ParticleFraction_%d.txt", SaveDirectory,Iterations);
				ofstream ParticleDebuger( temp_string, ios::out );
				
				for( i=0;i<Xnodes;i++)
				{
					for( j=0;j<Ynodes;j++)
					{
						ParticleDebuger<<*Fraction(i,j)<<"\t";
						
					}
					
					ParticleDebuger<<endl;
				}
				
				ParticleDebuger.close();	
				sprintf( temp_string, "%s/ParticleTissue_%d.txt", SaveDirectory,Iterations);
				ofstream ParticleTissue( temp_string, ios::out );
				
				for( i=0;i<Xnodes;i++)
				{
					for( j=0;j<Ynodes;j++)
					{
						ParticleTissue<<*TracerS(i,j)<<"\t";
						
					}
					
					ParticleTissue<<endl;
				}
				
				ParticleTissue.close();
				sprintf( temp_string, "%s/ParticleFlow_%d.txt", SaveDirectory,Iterations);
				ofstream ParticleFlow( temp_string, ios::out );
				
				for( i=0;i<Xnodes;i++)
				{
					for( j=0;j<Ynodes;j++)
					{
						ParticleFlow<<*Tracer(i,j)<<"\t";
						//if(j==200)
						//{
						//	cout << *Tracer(i,j)<<" 2 ";	
						//}
						
					}
					
					ParticleFlow<<endl;
				}
				
				ParticleFlow.close();
				
			}
		
		
		  
		
		
		
		
	}
	
	sprintf( temp_string, "%s/record.txt", SaveDirectory);
	ofstream record( temp_string, ios::out );
	
	
	record<<TimeStepMax<<"\t";
	record<<Iterations<<"\t";
	
	record<<endl;
	
	
	record.close();	
	
	
	cout <<"function TimeTransport finished"<<endl;
}



void VesselStructure::UpdateWallShearStress()
{

//cout <<"function UpdateWallShearStress running"<<endl; 

 extern Matrix WssX, WssY;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix ConductX, ConductY;
 extern Matrix FlowX, FlowY;
 extern int Xnodes, Ynodes;
 extern Matrix TracerX, TracerY;
 extern Matrix SrtX, SrtY;
 double PI = 3.141592654;
 double P_a = RetrieveDoubleParameter( ParameterFile, "P_a");
 double P_b = RetrieveDoubleParameter( ParameterFile, "P_b");
 double P_r = RetrieveDoubleParameter( ParameterFile, "P_r");
 double P_d = RetrieveDoubleParameter( ParameterFile, "P_d");
 MaxWss = 0;
 
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 int i, j;
 ofstream myfile14;
  myfile14.open ("debug2.txt");
  WssX = zeros(Xnodes, Ynodes);
  WssY = zeros(Xnodes, Ynodes);
  SrtX = zeros(Xnodes, Ynodes);
  SrtY = zeros(Xnodes, Ynodes);
  TracerX = zeros(Xnodes,Ynodes);
  TracerY = zeros(Xnodes,Ynodes);
 while( pCurrentVesselPoint)
 {
	i = pCurrentVesselPoint-> i;
	j = pCurrentVesselPoint-> j;

	//if(min_abs(*FlowX(i,j))<1e-15)
	//{*WssX(i, j) = 0.0;}
	//else
	{
	//if(j==200)
	//{
	if(i!=0&&(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5))
	{
	*WssX(i, j) = *BloodVesselRadiusXdir(i, j)/(2*NodeLength)* min_abs(*FlowX(i,j))/ *ConductX(i,j);//4.0 * *VisX(i, j) * min_abs( *FlowX(i, j) ) / ( PI * pow( *BloodVesselRadiusXdir(i, j), 3.0 ) ) ;
	*SrtX(i, j) = *WssX(i,j)/ (*VisX(i,j));
	*TracerX(i, j) = 1.0e6*2*PI* *BloodVesselRadiusXdir(i, j)*NodeLength* P_a* pow(P_d,1.57) *exp(-P_b*(1+P_r*pow(P_d,0.45))* *SrtX(i,j));
	
		//*TracerX(i, j) = *FlowX(i,j)*( 1.0- 1.0e6*2*PI* *BloodVesselRadiusXdir(i, j)*NodeLength* P_a* pow(P_d,1.57) *exp(-P_b*(1+P_r*pow(P_d,0.45))* *WssX(i,j)/ (*VisX(i,j))));


	}
	//}
	//else
	//{
	//*WssX(i, j) =15.0;
	//}
	}
	//if(min_abs(*FlowY(i,j))<1e-15)
	//{*WssY(i,j) = 0.0;}
	//else
	{
	//if(j==200)
	//{
	if(j!=0&&(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5))
	{
	*WssY(i, j) =*BloodVesselRadiusYdir(i, j)/(2*NodeLength)* min_abs(*FlowY(i,j))/ *ConductY(i,j);//4.0 * *VisY(i, j) * min_abs( *FlowY(i, j) ) / ( PI * pow( *BloodVesselRadiusYdir(i, j), 3.0 ) ) ;
	//*TracerY(i, j) = *FlowY(i,j)*(1.0- 1.0e6*2*PI* *BloodVesselRadiusYdir(i, j)*NodeLength*P_a* pow(P_d,1.57) *exp(-P_b*(1+P_r*pow(P_d,0.45))* *WssY(i,j)/(*VisY(i,j))));
	*SrtY(i, j) = *WssY(i,j)/(*VisY(i,j));
	*TracerY(i, j) = 1.0e6*2*PI* *BloodVesselRadiusYdir(i, j)*NodeLength*P_a* pow(P_d,1.57) *exp(-P_b*(1+P_r*pow(P_d,0.45))* *SrtY(i,j));
		
	}
	//}
	//else
	//{
	//*WssY(i, j)=15.0;
	//}
	}
	//cout<<"WssX"<<*WssX(i,j)<<endl;
	if(*WssX(i, j) > MaxWss)
	{
		MaxWss = *WssX(i, j);
		//myfile14 <<"MaxWssX"<<MaxWss<<"   "<<i<<", "<<j<<" "<<"RadiusX"<<*BloodVesselRadiusXdir(i, j)<<"NodeLength"<<NodeLength<<"Flow"<<*FlowX(i,j)<<"CondX"<<*ConductX(i,j)<<endl;
	}
	
	if(*WssY(i, j) > MaxWss)
	{
		MaxWss = *WssY(i, j);
		//myfile14 <<"MaxWssY"<<MaxWss<<"   "<<i<<", "<<j<<" "<<"RadiusY"<<*BloodVesselRadiusYdir(i, j)<<"NodeLength"<<NodeLength<<"Flow"<<*FlowY(i,j)<<"CondY"<<*ConductY(i,j)<<endl;

	}
	
	pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 }
	
myfile14.close();

cout <<"function UpdateWallShearStress finished"<<endl;
}

void VesselStructure::UpdateTracer_P(double Pa, double Pb, double Pd, Matrix& Paolo, Matrix& PaoloS)
{

//cout <<"function UpdateWallShearStress running"<<endl; 

 extern Matrix WssX, WssY;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix ConductX, ConductY;
 extern Matrix FlowX, FlowY;
 extern int Xnodes, Ynodes;
 extern Matrix TracerX, TracerY;
 extern Matrix PreExist;
 extern Matrix SrtX, SrtY;
 double PI = 3.141592654;
 double P_a = Pa;
 double P_b = Pb;
 double P_r = RetrieveDoubleParameter( ParameterFile, "P_r");
 double P_d = Pd;
  MaxWss = 0;
 
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 int i, j;
 ofstream myfile14;

  //WssX = zeros(Xnodes, Ynodes);
  //WssY = zeros(Xnodes, Ynodes);
  //SrtX = zeros(Xnodes, Ynodes);
  //SrtY = zeros(Xnodes, Ynodes);
  TracerX = zeros(Xnodes,Ynodes);
  TracerY = zeros(Xnodes,Ynodes);
	Paolo = zeros(Xnodes,Ynodes);
	PaoloS = zeros(Xnodes,Ynodes);
 while( pCurrentVesselPoint)
 {
	i = pCurrentVesselPoint-> i;
	j = pCurrentVesselPoint-> j;

	//if(min_abs(*FlowX(i,j))<1e-15)
	//{*WssX(i, j) = 0.0;}
	//else
	{
	//if(j==200)
	//{
	if(i!=0&&(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5))
	{
	//*WssX(i, j) = *BloodVesselRadiusXdir(i, j)/(2*NodeLength)* min_abs(*FlowX(i,j))/ *ConductX(i,j);//4.0 * *VisX(i, j) * min_abs( *FlowX(i, j) ) / ( PI * pow( *BloodVesselRadiusXdir(i, j), 3.0 ) ) ;
	//*SrtX(i, j) = *WssX(i,j)/ (*VisX(i,j));
	if(*PreExist(i,j) == 0)
	{
	*TracerX(i, j) = 100*1.0e6*2*PI* *BloodVesselRadiusXdir(i, j)*NodeLength* P_a* pow(P_d,1.57) *exp(-P_b*(1+P_r*pow(P_d,0.45))* *SrtX(i,j));
	//*TracerX(i, j) = *FlowX(i,j)*( 1.0- 1.0e6*2*PI* *BloodVesselRadiusXdir(i, j)*NodeLength* P_a* pow(P_d,1.57) *exp(-P_b*(1+P_r*pow(P_d,0.45))* *WssX(i,j)/ (*VisX(i,j))));
		
	}
	else
	{
	*TracerX(i, j) = 1.0e6*2*PI* *BloodVesselRadiusXdir(i, j)*NodeLength* P_a* pow(P_d,1.57) *exp(-P_b*(1+P_r*pow(P_d,0.45))* *SrtX(i,j));
	}
		
		*PaoloS(i,j)+= *TracerX(i,j);
		*Paolo(i,j)+= *TracerX(i,j)/(1.0e6*2*PI* *BloodVesselRadiusXdir(i, j)*NodeLength);

		if(*TracerX(i,j)>1)
		{
			*TracerX(i,j) =1;
		}
	
	}
	//}
	//else
	//{
	//*WssX(i, j) =15.0;
	//}
	}
	//if(min_abs(*FlowY(i,j))<1e-15)
	//{*WssY(i,j) = 0.0;}
	//else
	{
	//if(j==200)
	//{
	if(j!=0&&(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5))
	{
	//*WssY(i, j) =*BloodVesselRadiusYdir(i, j)/(2*NodeLength)* min_abs(*FlowY(i,j))/ *ConductY(i,j);//4.0 * *VisY(i, j) * min_abs( *FlowY(i, j) ) / ( PI * pow( *BloodVesselRadiusYdir(i, j), 3.0 ) ) ;
	//*TracerY(i, j) = *FlowY(i,j)*(1.0- 1.0e6*2*PI* *BloodVesselRadiusYdir(i, j)*NodeLength*P_a* pow(P_d,1.57) *exp(-P_b*(1+P_r*pow(P_d,0.45))* *WssY(i,j)/(*VisY(i,j))));
	//*SrtY(i, j) = *WssY(i,j)/(*VisY(i,j));
	if(*PreExist(i,j) == 0)
	{
	*TracerY(i, j) = 100*1.0e6*2*PI* *BloodVesselRadiusYdir(i, j)*NodeLength*P_a* pow(P_d,1.57) *exp(-P_b*(1+P_r*pow(P_d,0.45))* *SrtY(i,j));
	}
	else
	{
	*TracerY(i, j) = 1.0e6*2*PI* *BloodVesselRadiusYdir(i, j)*NodeLength*P_a* pow(P_d,1.57) *exp(-P_b*(1+P_r*pow(P_d,0.45))* *SrtY(i,j));
	}
		*PaoloS(i,j)+= *TracerY(i,j);
		*Paolo(i,j)+= *TracerY(i,j)/(1.0e6*2*PI* *BloodVesselRadiusYdir(i, j)*NodeLength);
		if(*TracerY(i,j)>1)
		{
			*TracerY(i,j) =1;
		}
		
	
	}
	//}
	//else
	//{
	//*WssY(i, j)=15.0;
	//}
	}
	//cout<<"WssX"<<*WssX(i,j)<<endl;
	
	pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 }
	


cout <<"function UpdateTracerP finished"<<endl;
}



void VesselStructure::UpdateRadius()
{
cout <<"function UpdateRadius running"<<endl;
 //--------- TauE calculated here is in Pascal ----------------

	//-----------------------------------IMPORTANT --------------------------------------
	// TauE(P) this law is given for P in mmHg and TauE in dyn/cm2
	// TauE is multiplied by 0.1 in order to convert dyn/cm2 into Pascal (1dyn/cm2=0.1Pa)
	// The pressures (NodalPressures) are multiplied by 0.007501 in order to convert 
	// Pascal into mmHg (1Pa=0.007501mmHg)
		double convPa=0.007501;
		extern char* ParameterFile;
	//-----------------------------------------------------------------------------------
 extern int Xnodes, Ynodes; 
 extern Matrix WssX, WssY;
 extern Matrix FlowX, FlowY;
 extern Matrix HaematocritLevel;
 extern Matrix TotalPressure;
 extern Matrix Pressure;
 extern Matrix HomPressure;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
 extern Matrix BloodNodePressure;
 extern Matrix VesselAge;
 double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
 //extern Matrix DirectionX, DirectionY;
	extern bool ResetPressure;
 extern Matrix BloodVesselPressureX, BloodVesselPressureY; //This is contained in older code which I don't know it's neccesity. But anyway...
 //Also the older one " CalculatePressureInVessel" is geometrically BLIND.
 Matrix TauEX, TauEY;
 extern Matrix Stability;
	extern double t;
 TauEX = zeros( Xnodes, Ynodes);
 TauEY = zeros( Xnodes, Ynodes);
    //double Sm, Swss, Sp;
	extern Matrix SmX,SmY,SwssX,SwssY,SpX,SpY,SsX,SsY,SvarX,SvarY;
   SwssX = zeros( Xnodes, Ynodes);
   SmX = zeros( Xnodes, Ynodes);
   SpX = zeros( Xnodes, Ynodes);
   SsX = zeros( Xnodes, Ynodes);
   SvarX = zeros( Xnodes, Ynodes);
	SwssY= zeros( Xnodes, Ynodes);
	SmY = zeros( Xnodes, Ynodes);
	SpY = zeros( Xnodes, Ynodes);
	SsY = zeros( Xnodes, Ynodes);
	SvarY = zeros( Xnodes, Ynodes);
	extern double dt;
	extern Matrix CollapseClock;
	double coeffks;
	double coeffkp;  
	double coeffkm;
	double coeffkps;
	double Pmin;
	double Pmax;
	coeffks = RetrieveDoubleParameter( ParameterFile , "coeffks");
    coeffkp = RetrieveDoubleParameter( ParameterFile , "coeffkp");
    coeffkm = RetrieveDoubleParameter( ParameterFile , "coeffkm");
	coeffkps = RetrieveDoubleParameter( ParameterFile , "coeffkps");
	Pmin = RetrieveDoubleParameter( ParameterFile , "Pmin");
	Pmax = RetrieveDoubleParameter( ParameterFile , "Pmax");
	
	double TauRef=0.103; //(dyn/cm2)
	double FlowRef=1.4e-13;//1.0e-11;//FlowRef=3.3e-13;//3.3e-12;//1.9096e-11; //m3/s (Flow in PV)

	   double RadiusLimitInf=RetrieveDoubleParameter( ParameterFile , "RadiusLimitInf" ); 
	double RadiusLimitSup=RetrieveDoubleParameter( ParameterFile , "RadiusLimitSup" );
	double RadiusVariation;
    int i=0;
	int j=0;
	int v=0;
	ResetPressure = 0;
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;

	
	if(t<=0.05)		
	{
		while( pCurrentVesselPoint)
		{
			
			if((pCurrentVesselPoint->AmIArtery)==0)
				
			{
				i = pCurrentVesselPoint-> i;
				
				j = pCurrentVesselPoint-> j;
				
				
				
				
				
				
				
				
				
				
				
				
				
				/*if(*TotalPressure(i,j)>=(0.1*HomPressure))
				{
					*BloodVesselRadiusXdir(i,j) = 0;
					*BloodVesselRadiusYdir(i,j) = 0;
					ResetPressure = 1;
					*CollapseClock(i,j)+= dt;
				}
				else*/
			
					
					if(*CollapseClock(i,j)>0)
					{
						*CollapseClock(i,j) = 0;
						*BloodVesselRadiusXdir(i,j) = RadiusLimitInf;
						*BloodVesselRadiusYdir(i,j) = RadiusLimitInf;
					}
					else{
						
						*CollapseClock(i,j) = 0;
						if(i!=0&&min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5)
						{
							
							
							
							
														//cout<<"RadiusVariation"<<RadiusVariation;
							*BloodVesselRadiusXdir(i, j) = 6.0e-6;
														
							
						}
						// Y direction
						
						
						if(j!=0&&min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5)
						{
							
							
							*BloodVesselRadiusYdir(i, j) = 6.0e-6;
							
							
														
						}
					}
					
				}
				
				
				
								
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
			
		}
		cout <<"function UpdateRadius finished"<<endl;

	
	}
	else
	{
  //cout<<pFirstVesselPoint->i<<"  "<<pFirstVesselPoint->j<<endl;
 while( pCurrentVesselPoint)
 {
 
	if((pCurrentVesselPoint->AmIArtery)==0)
	
{
	i = pCurrentVesselPoint-> i;
	
	j = pCurrentVesselPoint-> j;
	
	
	
	
	
	
	
	
	/*if(*TotalPressure(i,j)>=(0.1*HomPressure))
	{
		*BloodVesselRadiusXdir(i,j) = 0;
		*BloodVesselRadiusYdir(i,j) = 0;
		*CollapseClock(i,j)+= dt;
	}
	else*/
	
				
		if(*CollapseClock(i,j)>0)
		{
			
			//if(*TotalPressure(i,j)>=Pmin)
			if(*Pressure(i,j)>=Pmin)
			{
				*BloodVesselRadiusXdir(i,j) = 0;
				*BloodVesselRadiusYdir(i,j) = 0;
				*CollapseClock(i,j)+= dt;
				
				
			}
			else 
			{
				*CollapseClock(i,j) = 0;
				*BloodVesselRadiusXdir(i,j) = RadiusLimitInf;				
				*BloodVesselRadiusYdir(i,j) = RadiusLimitInf;
				
			}

			
			
		}
	else if(*BloodNodePressure(i,j)<10)
	  { 
		  *BloodVesselRadiusXdir(i,j) = 0;
		  *BloodVesselRadiusYdir(i,j) = 0;
		  *CollapseClock(i,j)+= dt;
		  
	  }
	else
		{
			
			
			
			
  if(i!=0&&min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5)
  {

  

   
	 if(convPa * *BloodVesselPressureX(i, j) < 10.0)//for small pressure
  {
	*BloodVesselPressureX(i, j) = 10.1 / convPa;
  }
 *TauEX(i, j) = 0.1 * ( 100.0 - 86.0 * exp( -5000 * pow( log10( log10( convPa * *BloodVesselPressureX(i, j) ) ), 5.4 ) ) ) ;//why is here 0.1??


 if(*FlowX(i,j)==0||(*HaematocritLevel(i-1, j)+*HaematocritLevel(i, j))/2<0.001)
 {
 *SmX(i,j) = 0;//coeffkm * log10( ( FlowRef / ( min_abs( *FlowX(i, j) ) * *HemX(i, j) ) ) + 1.0 );
 }
 else
 {
 *SmX(i,j) = coeffkm * log10( ( FlowRef / ( min_abs( *FlowX(i, j) ) * (*HaematocritLevel(i-1, j)+*HaematocritLevel(i, j))/2 ) ) + 1.0 );
 }
 
// cout<<"Sm"<<Sm;
 *SpX(i,j) = coeffkp * log10(  *TauEX(i, j) );
// cout<<"SpX"<<Sp;
 *SwssX(i,j) = log10(  *WssX(i, j)*10 + TauRef );
// cout<<"SwssX"<<Swss;
 //getchar();
 
	  //if(*TotalPressure(i,j)>=Pmin)
	  if(*Pressure(i,j)>=Pmin)
	  {
		  //if(*TotalPressure(i,j)<Pmax)
		  if(*Pressure(i,j)<Pmax)
		  {
			 // coeffkps *= (*TotalPressure(i,j)-Pmin)/(Pmax-Pmin);			 
			  *SsX(i,j) = coeffkps*(*Pressure(i,j)-Pmin)/(Pmax-Pmin);
		  }
		  else
		  {
		     *SsX(i,j) = coeffkps;
		  }		  
		  
 *SsX(i,j) += coeffks;
		  
 *SvarX(i,j) = *BloodVesselRadiusXdir(i, j) * AngioDeltaT * ( *SwssX(i,j) - *SpX(i,j) + *SmX(i,j) - *SsX(i,j) );// Adaptionmodify
	  }
	  else
	  {
	 *SsX(i,j) = coeffks;
 
     *SvarX(i,j) = *BloodVesselRadiusXdir(i, j) * AngioDeltaT * ( *SwssX(i,j) - *SpX(i,j) + *SmX(i,j) - *SsX(i,j) );// Adaptionmodify
	  }
 //cout<<"RadiusVariation"<<RadiusVariation;
 *BloodVesselRadiusXdir(i, j) += *SvarX(i,j);
 //cout<<"BVI"<<*BloodVesselRadiusXdir(i, j);

if (*BloodVesselRadiusXdir(i, j) < RadiusLimitInf )
 {
	 //if(*TotalPressure(i,j)>=Pmin)
	 if(*Pressure(i,j)>=Pmin)
	 {
		 *BloodVesselRadiusXdir(i, j) = 0;
		 *CollapseClock(i,j) += 0.5*dt;
		 ResetPressure = 1;
	 }
	 else
	 {
     *BloodVesselRadiusXdir(i, j) = RadiusLimitInf;
		 *CollapseClock(i,j) = 0;
	 }
	 
	 
 }
	  
	  
	  
//todday
if (*BloodVesselRadiusXdir(i, j) > RadiusLimitSup )
 {
   *BloodVesselRadiusXdir(i, j) = RadiusLimitSup;
 }

 
 }
// Y direction
			
			
			


  if(j!=0&&min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5)
 {

//getchar();
  if(convPa * *BloodVesselPressureY(i, j) < 10.0)
 {
	*BloodVesselPressureY(i, j) = 10.1 / convPa;
 }
 
 //getchar();
 *TauEY(i, j) = 0.1 * ( 100.0 - 86.0 * exp( -5000 * pow( log10( log10( convPa * *BloodVesselPressureY(i, j) ) ), 5.4 ) ) ) ;//why is here 0.1??
 if(*FlowY(i,j)==0||(*HaematocritLevel(i, j-1)+*HaematocritLevel(i, j))/2<0.001)
 {
 *SmY(i,j) = 0.0;//coeffkm * log10( ( FlowRef / ( min_abs( *FlowY(i, j) ) * *HemY(i, j) ) ) + 1.0 );
 }
 else
 {
 *SmY(i,j) = coeffkm * log10( ( FlowRef / ( min_abs( *FlowY(i, j) ) * (*HaematocritLevel(i, j-1)+*HaematocritLevel(i, j))/2 ) ) + 1.0 );
 }
 *SpY(i,j) = coeffkp * log10(  *TauEY(i, j) );
 //cout<<"SpY"<<Sp;
 *SwssY(i,j) = log10( *WssY(i,j)*10 + TauRef );
 //cout<<"WssY"<<*WssY(i,j)<<endl;
 // cout<<"Swss"<<Swss<<endl<<"Sm"<<Sm<<endl<<"Sp"<<Sp<<endl<<"coeffks"<<coeffks<<endl<<endl;
	 
	 
	 //if(*TotalPressure(i,j)>=Pmin)
	 if(*Pressure(i,j)>=Pmin)
	 {
		 //if(*TotalPressure(i,j)<Pmax)
		  if(*Pressure(i,j)<Pmax)
		  {
			 // coeffkps *= (*TotalPressure(i,j)-Pmin)/(Pmax-Pmin);			 
			  *SsY(i,j) = coeffkps*(*Pressure(i,j)-Pmin)/(Pmax-Pmin);
		  }
		  else
		  {
		     *SsY(i,j) = coeffkps;
		  }		  
		  
     *SsY(i,j) += coeffks;
     *SvarY(i,j) = *BloodVesselRadiusYdir(i, j) * AngioDeltaT * ( *SwssY(i,j) - *SpY(i,j) + *SmY(i,j) - *SsY(i,j) );// Adaptionmodify


	 }
	 else
	 {
	*SsY(i,j) = coeffks;
 
     *SvarY(i,j) = *BloodVesselRadiusYdir(i, j) * AngioDeltaT * ( *SwssY(i,j) - *SpY(i,j) + *SmY(i,j) - *SsY(i,j) );// Adaptionmodify
	 }
	 
 
 *BloodVesselRadiusYdir(i, j) += *SvarY(i,j);



	 
	 
	 
	 if (*BloodVesselRadiusYdir(i, j) < RadiusLimitInf )
	 {
		 //if(*TotalPressure(i,j)>=Pmin)
		 if(*Pressure(i,j)>=Pmin)
		 {
			 *BloodVesselRadiusYdir(i, j) = 0;
			 *CollapseClock(i,j) += 0.5*dt;
			 ResetPressure = 1;
		 }
		 else
		 {
			 *BloodVesselRadiusYdir(i, j) = RadiusLimitInf;
			 *CollapseClock(i,j) = 0;
		 }
		 
		 
	 }

 if (*BloodVesselRadiusYdir(i, j) > RadiusLimitSup )
{
  *BloodVesselRadiusYdir(i, j) = RadiusLimitSup;
}
 
}
}

}


	
	
	
	
	
	
	
	
	
	
	
	
	
 

  

pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 	
 }
 cout <<"function UpdateRadius finished"<<endl;
	}

	}


void VesselStructure::UpdateRadius_Cell()
{
cout <<"function UpdateRadius_cell running"<<endl;
 //--------- TauE calculated here is in Pascal ----------------

	//-----------------------------------IMPORTANT --------------------------------------
	// TauE(P) this law is given for P in mmHg and TauE in dyn/cm2
	// TauE is multiplied by 0.1 in order to convert dyn/cm2 into Pascal (1dyn/cm2=0.1Pa)
	// The pressures (NodalPressures) are multiplied by 0.007501 in order to convert 
	// Pascal into mmHg (1Pa=0.007501mmHg)
	
		extern char* ParameterFile;
	//-----------------------------------------------------------------------------------
 extern int Xnodes, Ynodes; 
 extern Matrix WssX, WssY;
 extern Matrix FlowX, FlowY;
 extern Matrix SproutTips;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselRadius;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY, BloodVesselIndicator;
 extern Matrix BloodNodePressure;
 extern Matrix VesselAge;
 extern Matrix TAF;
 extern Matrix VesselCells;
 
 double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");

  double min_C = RetrieveDoubleParameter( ParameterFile , "min_C");
 //extern Matrix DirectionX, DirectionY;
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 //Matrix PressureAverageX, PressureAverageY; //This is contained in older code which I don't know it's neccesity. But anyway...

	double RadiusLimitInf=RetrieveDoubleParameter( ParameterFile , "RadiusLimitInf" ); 
	double RadiusLimitSup=RetrieveDoubleParameter( ParameterFile , "RadiusLimitSup" );
	double Rmin=RetrieveDoubleParameter( ParameterFile , "Rmin" );
    double r;
    int i, j;
 //BloodVesselRadiusXdir = zeros(Xnodes,Ynodes);
 //BloodVesselRadiusYdir = zeros(Xnodes,Ynodes);
 //BloodVesselRadius = zeros(Xnodes,Ynodes);
 
 
  while( pCurrentVesselPoint)
 {
 
	i = pCurrentVesselPoint-> i;
	j = pCurrentVesselPoint-> j;
r =0;
 if(pCurrentVesselPoint-> MyAge < VesselAgeInf)
{
 if(*BloodVesselIndicator(i,j)>0.5)
 {
 if(*VesselCells(i,j)>0)
 {
 r = min_C* *VesselCells(i,j)/(*BloodVesselIndicator(i,j));
 }
 cout<<"r"<<r;
 if ( r>= Rmin)
 {
   *BloodVesselRadius(i, j) = r;
   if(*SproutTips(i,j)==0)
   {
   pCurrentVesselPoint-> MyAge = VesselAgeInf;
   }
 }
 else
 {
 *BloodVesselRadius(i, j) = r;
  pCurrentVesselPoint-> MyAge = 0;
 }
 
 //cout<<"BVI"<<*BloodVesselRadiusXdir(i, j);
 }
//if (*BloodVesselRadius(i, j) < RadiusLimitInf )
// {
//   *BloodVesselRadius(i, j) = 0;
// }
//todday

}



pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 	
 }//update point by point
 
 
 
 
 
 
 pCurrentVesselPoint = pFirstVesselPoint;

 
 while( pCurrentVesselPoint)
 {
 
	i = pCurrentVesselPoint-> i;
	j = pCurrentVesselPoint-> j;
 if(pCurrentVesselPoint-> MyAge < VesselAgeInf)
 {

  if(i!=0&&(min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))
  {
  
  
  *BloodVesselRadiusXdir(i, j) = (*BloodVesselRadius(i-1, j)+*BloodVesselRadius(i, j))/2;
  
  
  
  
  
   if (*BloodVesselRadiusXdir(i, j) < RadiusLimitInf )
 {
   *BloodVesselRadiusXdir(i, j) = 0;
 }
//todday
if (*BloodVesselRadiusXdir(i, j) > RadiusLimitSup )
 {
   *BloodVesselRadiusXdir(i, j) = RadiusLimitSup;
 }
  }//if connected X
  
  if(i!=Xnodes-1&&(min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
  {
  
  
  *BloodVesselRadiusXdir(i+1, j) = (*BloodVesselRadius(i, j)+*BloodVesselRadius(i+1, j))/2;
  
  
  
  
  
   if (*BloodVesselRadiusXdir(i+1, j) < RadiusLimitInf )
 {
   *BloodVesselRadiusXdir(i+1, j) = 0;
 }
//todday
if (*BloodVesselRadiusXdir(i+1, j) > RadiusLimitSup )
 {
   *BloodVesselRadiusXdir(i+1, j) = RadiusLimitSup;
 }
  }//if conn

  if(j!=0&&(min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
  {




*BloodVesselRadiusYdir(i, j) = (*BloodVesselRadius(i, j-1)+*BloodVesselRadius(i, j))/2;


if (*BloodVesselRadiusYdir(i, j) < RadiusLimitInf )
  {
   *BloodVesselRadiusYdir(i, j) = 0;
  }

 if (*BloodVesselRadiusYdir(i, j) > RadiusLimitSup )
{
  *BloodVesselRadiusYdir(i, j) = RadiusLimitSup;
}
 
  }//if connected Y
  
  if(j!=Ynodes-1&&(min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
  {




*BloodVesselRadiusYdir(i, j+1) = (*BloodVesselRadius(i, j)+*BloodVesselRadius(i, j+1))/2;


if (*BloodVesselRadiusYdir(i, j+1) < RadiusLimitInf )
  {
   *BloodVesselRadiusYdir(i, j+1) = 0;
  }

 if (*BloodVesselRadiusYdir(i, j+1) > RadiusLimitSup )
{
  *BloodVesselRadiusYdir(i, j+1) = RadiusLimitSup;
}
 
  }//if connected Y

  
 
 }
pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 	
 }
  cout <<"function UpdateRadius_cell finished"<<endl;

}


 void VesselStructure:: UpdateConductivity()
{
	//cout <<"function UpdateConductivity running"<<endl;
	extern Matrix ConductX, ConductY;
	
	extern Matrix BloodVesselRadiusXdir;
	extern Matrix BloodVesselRadiusYdir;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix CollapseClock;
	extern Matrix VisX, VisY;
	extern Matrix TAF;
	extern int Xnodes,Ynodes;
	double pi = 3.141592654;
	double MaxConduct= 0;
	double Perm = RetrieveDoubleParameter( ParameterFile , "Perm");
	ConductX = zeros(Xnodes, Ynodes);
	ConductY = zeros(Xnodes, Ynodes);
	
	
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
    
	
	while(pCurrentVesselPoint)
	{
		int i = pCurrentVesselPoint->i;
		int j = pCurrentVesselPoint->j;
		if(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
			
			
		    *ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j), 4 ) / ( 8.0 * *VisX(i, j) * NodeLength );
			if(*ConductX(i, j)>MaxConduct)
			{MaxConduct=*ConductX(i, j);}
			
		}
			
			
		
		if(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
			
			
		    *ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j), 4 ) / ( 8.0 * *VisY(i, j) * NodeLength );
		if(*ConductY(i, j)>MaxConduct)
	    {MaxConduct=*ConductY(i, j);}
		
			
		}
		//cout<<*ConductX(i,j)<<endl;
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
			
	 }

   

	cout <<"function UpdateConductivity finished"<<endl;
}
void VesselStructure::TransOutPutThresholdFlow()
{
int i, j, i_minus, j_minus, i_plus, j_plus;
extern int Xnodes, Ynodes;
extern Matrix BloodVesselIndicator, BloodVesselIndicatorX, BloodVesselIndicatorY;
extern Matrix Flow, FlowX, FlowY;
extern Matrix Vis, VisX, VisY;
extern Matrix BloodVesselRadius, BloodVesselRadiusXdir, BloodVesselRadiusYdir;
Flow = zeros(Xnodes,Ynodes);
Vis = zeros(Xnodes,Ynodes);
BloodVesselRadius = zeros(Xnodes,Ynodes);
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
while(pCurrentVesselPoint)
{
i=pCurrentVesselPoint->i;
j=pCurrentVesselPoint->j;



		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{       
			if(*FlowX(i, j) >1e-15)
			{
			*Flow(i,j) += min_abs( *FlowX(i, j));
			}
			*Vis(i,j) += *VisX(i,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i,j);
			
						
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		if(*FlowX(i_plus, j)*(-1.0) > 1e-15)
			{
			*Flow(i,j) += min_abs( *FlowX(i_plus, j) );
			}
			*Vis(i,j) += *VisX(i_plus,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i_plus,j);
						
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
			if(*FlowY(i, j) > 1e-15)
			{
			*Flow(i,j) += min_abs( *FlowY(i, j) );
			}
			
			*Vis(i,j) += *VisY(i,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j);
	
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
			if(*FlowY(i, j_plus)*(-1.0) > 1e-15)
			{
			*Flow(i,j) += min_abs( *FlowY(i, j_plus) );
		//cout<<" Flow "<<*Flow(i,j);
			}
			*Vis(i,j) += *VisY(i,j_plus);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j_plus);
						
	}
	
	*Vis(i,j) /= *BloodVesselIndicator(i,j);
	*BloodVesselRadius(i,j) /= *BloodVesselIndicator(i,j);
 // cout<<"BVR"<<*BloodVesselRadius(i,j);




pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
}

 //SetBoundary flow to 0
	 


}


void VesselStructure::TransOutPut()
{
int i, j, i_minus, j_minus, i_plus, j_plus;
extern int Xnodes, Ynodes;
extern double TotalIn;
extern Matrix BloodVesselIndicator, BloodVesselIndicatorX, BloodVesselIndicatorY;
extern Matrix Flow, FlowX, FlowY;
extern Matrix Vis, VisX, VisY;
extern Matrix WssX, WssY, SrtX, SrtY;
extern Matrix Wss,ShearRate;
extern Matrix Swss, Sm,Sp, Ss, Svar;
extern Matrix SwssX, SmX,SpX, SsX, SvarX;
extern Matrix SwssY, SmY,SpY, SsY, SvarY;
extern Matrix Macrof, MacrofD, TracerT;
extern Matrix MapIndicator, MapRadius, MapFlow, MapPressure, MapVesselCells, MapSproutTips, Tracer,TracerX,TracerY, TracerS, Wss;
extern Matrix BloodVesselRadius, BloodVesselRadiusXdir, BloodVesselRadiusYdir;
	
	extern Matrix HomPressure;
	extern Matrix InterPressure, Permeability, Conduct;
	//Leak = zeros(Xnodes,Ynodes);
	Swss = zeros( Xnodes, Ynodes);
	Sm = zeros( Xnodes, Ynodes);
	Sp = zeros( Xnodes, Ynodes);
	Ss = zeros( Xnodes, Ynodes);
	Svar = zeros( Xnodes, Ynodes);
Flow = zeros(Xnodes,Ynodes);
Vis = zeros(Xnodes,Ynodes);

TotalIn = 0;
BloodVesselRadius = zeros(Xnodes,Ynodes);
//Tracer = zeros(Xnodes, Ynodes);
//TracerS = zeros(Xnodes, Ynodes);
Wss = zeros(Xnodes, Ynodes);
ShearRate = zeros(Xnodes, Ynodes);
extern Matrix BloodNodePressure, BloodVesselPressureX, BloodVesselPressureY;
extern Matrix SproutTips, VesselCells;
double pi = 3.141592654;
MapIndicator = zeros(2*Xnodes-1, 2*Ynodes-1);
MapVesselCells = zeros(2*Xnodes-1, 2*Ynodes-1);
MapSproutTips = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapFlow = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapRadius = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapPressure = zeros(2*Xnodes-1, 2*Ynodes-1);

 


VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
while(pCurrentVesselPoint)
{
i=pCurrentVesselPoint->i;
j=pCurrentVesselPoint->j;

if(pCurrentVesselPoint->AmIArtery)
{
	if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{       
	
			
			
			*Flow(i,j) += min_abs( *FlowX(i, j));
			*Vis(i,j) += *VisX(i,j);
			*Wss(i,j) += *WssX(i,j);
			*ShearRate(i,j) += *SrtX(i,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i,j);
			//*Tracer(i,j) += *TracerX(i,j);
		    *Swss(i,j) += *SwssX(i,j);
		    *Sm(i,j) += *SmX(i,j);
		    *Sp(i,j) += *SpX(i,j);
			*Ss(i,j) += *SsX(i,j);	
		    *Svar(i,j) += *SvarX(i,j);
			
						
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
			*Flow(i,j) += min_abs( *FlowX(i_plus, j) );
			
			*Vis(i,j) += *VisX(i_plus,j);
			*Wss(i,j) += *WssX(i_plus,j);
			*ShearRate(i,j) += *SrtX(i_plus,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i_plus,j);
			//*Tracer(i,j) += *TracerX(i_plus,j);
		*Swss(i,j) += *SwssX(i_plus,j);
		*Sm(i,j) += *SmX(i_plus,j);
		*Sp(i,j) += *SpX(i_plus,j);
		*Ss(i,j) += *SsX(i_plus,j);	
		*Svar(i,j) += *SvarX(i_plus,j);
						
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
			
			*Flow(i,j) += min_abs( *FlowY(i, j) );
			
			
			*Vis(i,j) += *VisY(i,j);
			*Wss(i,j) += *WssY(i,j);
			*ShearRate(i,j) += *SrtY(i,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j);
			//*Tracer(i,j) += *TracerY(i,j);
		*Swss(i,j) += *SwssY(i,j);
		*Sm(i,j) += *SmY(i,j);
		*Sp(i,j) += *SpY(i,j);
		*Ss(i,j) += *SsY(i,j);	
		*Svar(i,j) += *SvarY(i,j);
	
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
			*Flow(i,j) += min_abs( *FlowY(i, j_plus) );
		//cout<<" Flow "<<*Flow(i,j);
			
			*Vis(i,j) += *VisY(i,j_plus);
			*Wss(i,j) += *WssY(i,j_plus);
			*ShearRate(i,j) += *SrtY(i,j_plus);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j_plus);
			//*Tracer(i,j) += *TracerY(i,j_plus);
		*Swss(i,j) += *SwssY(i,j_plus);
		*Sm(i,j) += *SmY(i,j_plus);
		*Sp(i,j) += *SpY(i,j_plus);
		*Ss(i,j) += *SsY(i,j_plus);	
		*Svar(i,j) += *SvarY(i,j_plus);
						
	}
	
	TotalIn +=*Flow(i,j);
	
	if(*BloodVesselIndicator(i,j)>0)
	{
	*Vis(i,j) /= *BloodVesselIndicator(i,j);
	*BloodVesselRadius(i,j) /= *BloodVesselIndicator(i,j);
	*Wss(i,j) /= *BloodVesselIndicator(i,j);
	*ShearRate(i,j) /= *BloodVesselIndicator(i,j);
		*Swss(i,j) /= *BloodVesselIndicator(i,j);
		*Sm(i,j) /= *BloodVesselIndicator(i,j);
		*Sp(i,j) /= *BloodVesselIndicator(i,j);
		*Ss(i,j) /= *BloodVesselIndicator(i,j);	
		*Svar(i,j) /= *BloodVesselIndicator(i,j);
		

	//*Tracer(i,j) /= *BloodVesselIndicator(i,j);
	//*TracerS(i,j) = pi* *BloodVesselRadius(i,j)* *Tracer(i,j);
	}
 // cout<<"BVR"<<*BloodVesselRadius(i,j);

}
else
{

		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{       
	
			
			if(*FlowX(i, j) >0)
			{
			*Flow(i,j) += min_abs( *FlowX(i, j));
			}
			*Vis(i,j) += *VisX(i,j);
			*Wss(i,j) += *WssX(i,j);
			*ShearRate(i,j) += *SrtX(i,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i,j);
			//*Tracer(i,j) += *TracerX(i,j);
		*Swss(i,j) += *SwssX(i,j);
		*Sm(i,j) += *SmX(i,j);
		*Sp(i,j) += *SpX(i,j);
		*Ss(i,j) += *SsX(i,j);	
		*Svar(i,j) += *SvarX(i,j);
						
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		if(*FlowX(i_plus, j)*(-1.0) > 0)
			{
			*Flow(i,j) += min_abs( *FlowX(i_plus, j) );
			}
			*Vis(i,j) += *VisX(i_plus,j);
			*Wss(i,j) += *WssX(i_plus,j);
			*ShearRate(i,j) += *SrtX(i_plus,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i_plus,j);
			//*Tracer(i,j) += *TracerX(i_plus,j);
		*Swss(i,j) += *SwssX(i_plus,j);
		*Sm(i,j) += *SmX(i_plus,j);
		*Sp(i,j) += *SpX(i_plus,j);
		*Ss(i,j) += *SsX(i_plus,j);	
		*Svar(i,j) += *SvarX(i_plus,j);
		
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
			if(*FlowY(i, j) > 0)
			{
			*Flow(i,j) += min_abs( *FlowY(i, j) );
			}
			
			*Vis(i,j) += *VisY(i,j);
			*Wss(i,j) += *WssY(i,j);
			*ShearRate(i,j) += *SrtY(i,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j);
			//*Tracer(i,j) += *TracerY(i,j);
		*Swss(i,j) += *SwssY(i,j);
		*Sm(i,j) += *SmY(i,j);
		*Sp(i,j) += *SpY(i,j);
		*Ss(i,j) += *SsY(i,j);	
		*Svar(i,j) += *SvarY(i,j);
		
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
			if(*FlowY(i, j_plus)*(-1.0) > 0)
			{
			*Flow(i,j) += min_abs( *FlowY(i, j_plus) );
		//cout<<" Flow "<<*Flow(i,j);
			}
			*Vis(i,j) += *VisY(i,j_plus);
			*Wss(i,j) += *WssY(i,j_plus);
			*ShearRate(i,j) += *SrtY(i,j_plus);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j_plus);
			//*Tracer(i,j) += *TracerY(i,j_plus);
		*Swss(i,j) += *SwssY(i,j_plus);
		*Sm(i,j) += *SmY(i,j_plus);
		*Sp(i,j) += *SpY(i,j_plus);
		*Ss(i,j) += *SsY(i,j_plus);	
		*Svar(i,j) += *SvarY(i,j_plus);
		
		
	}
	
	if(*BloodVesselIndicator(i,j)>0)
	{
	*Vis(i,j) /= *BloodVesselIndicator(i,j);
	*BloodVesselRadius(i,j) /= *BloodVesselIndicator(i,j);
	*Wss(i,j) /= *BloodVesselIndicator(i,j);
	*ShearRate(i,j) /= *BloodVesselIndicator(i,j);
		
	//*Tracer(i,j) /= *BloodVesselIndicator(i,j);
	//*TracerS(i,j) = pi* *BloodVesselRadius(i,j)* *Tracer(i,j);
		*Swss(i,j) /= *BloodVesselIndicator(i,j);
		*Sm(i,j) /= *BloodVesselIndicator(i,j);
		*Sp(i,j) /= *BloodVesselIndicator(i,j);
		*Ss(i,j) /= *BloodVesselIndicator(i,j);	
		*Svar(i,j) /= *BloodVesselIndicator(i,j);
	}
 // cout<<"BVR"<<*BloodVesselRadius(i,j);

}


pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
}

 //SetBoundary flow to 0
 for(int ii =0; ii< 2*Xnodes-1 ; ii++)
{
 
 for(int jj =0; jj< 2*Ynodes-1 ; jj++)
 {
 
 if(ii%2&&jj%2)
 {
if(min_abs(*BloodVesselIndicator(ii/2,jj/2))>0.5)
 {
 *MapIndicator(ii,jj) = 1;
 }	
 *MapRadius(ii,jj) = *BloodVesselRadius(ii/2,jj/2);
 *MapFlow(ii,jj) =*Flow(ii/2,jj/2);
 *MapPressure(ii,jj) = *BloodNodePressure(ii/2,jj/2);
 *MapVesselCells(ii,jj) = *VesselCells(ii/2,jj/2);
 *MapSproutTips(ii,jj) = *SproutTips(ii/2,jj/2);
 
 }
 else if(ii%2&&(!(jj%2)))
 {
 if(min_abs(*BloodVesselIndicatorY(ii/2,(jj+1)/2))>1.5||*BloodVesselIndicatorY(ii/2,(jj+1)/2)<-0.5)
 {
 *MapIndicator(ii,jj) = 1;
 }
 
 *MapRadius(ii,jj) = *BloodVesselRadiusYdir(ii/2,(jj+1)/2);
 *MapFlow(ii,jj) = min_abs(*FlowY(ii/2,(jj+1)/2));
 *MapPressure(ii,jj) = *BloodVesselPressureY(ii/2,(jj+1)/2);
 }
 else if((!(ii%2))&&jj%2)
 {
 if(min_abs(*BloodVesselIndicatorX( (ii+1)/2,(jj)/2 ))>1.5||*BloodVesselIndicatorX( (ii+1)/2,(jj)/2 )<-0.5)
 {
 *MapIndicator(ii,jj) = 1;
 }
 
*MapRadius(ii,jj) = *BloodVesselRadiusXdir((ii+1)/2,(jj)/2);
 *MapFlow(ii,jj) = min_abs(*FlowX((ii+1)/2,(jj)/2));
*MapPressure(ii,jj) = *BloodVesselPressureX((ii+1)/2,(jj)/2);
 }
 
 }
 
 
 }
	 
	 
	 
	 


}



 void VesselStructure::RegenerateFlow()
{
extern double FlowMax;
extern Matrix Flow;
extern Matrix HaematocritLevel;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix Phi;
extern Matrix PreExist;
extern double t;
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
int i,j;

FlowMax = 0.0;


while(pCurrentVesselPoint)
{
i=pCurrentVesselPoint->i;
j=pCurrentVesselPoint->j;

if((*Phi(i,j)<0) && (*PreExist(i,j)==0) && (*Flow(i,j)>FlowMax))
{
FlowMax = *Flow(i,j);
}


pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
}

cout<<"FlowMax"<<FlowMax<<endl;


if(FlowMax>0.0)
{
pCurrentVesselPoint = pFirstVesselPoint;
while(pCurrentVesselPoint)
{
i=pCurrentVesselPoint->i;
j=pCurrentVesselPoint->j;



*Flow(i,j)= *Flow(i,j)/FlowMax;	
	//if (*Flow(i,j)>0.1)
	//{
	 // cout << *Flow(i,j)<<endl;
	//}
	
		
pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
}
}
else
{
pCurrentVesselPoint = pFirstVesselPoint;
while(pCurrentVesselPoint)
{
i=pCurrentVesselPoint->i;
j=pCurrentVesselPoint->j;

*Flow(i,j)= 0.0;	
	//if (*Flow(i,j)>0.1)
	//{
	 // cout << *Flow(i,j)<<endl;
	//}
	
		
pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
}
}

 //SetBoundary flow to 0
 


}

/***********


void VesselStructure::Kill(Matrix& Pressure)
{

//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix SproutTips;
extern Matrix VesselAge;
extern Matrix SproutAge;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern Matrix ECM;
extern int Xnodes, Ynodes;

int iminus,iplus,jminus,jplus;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
int LoopLength = VesselListLength; 

}
***********/
void VesselStructure::InterAngioPressureUpdate(int MaxIterations, double Tolerance)
 {
 extern Matrix ConductX,ConductY;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
 extern Matrix BloodNodePressure;
 extern Matrix LymIndicator;
 extern Matrix InterPressure;
 extern Matrix Permeability;
 extern Matrix Conduct;
 extern Matrix X,Y;
 extern int Xnodes, Ynodes;

 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;//initialize the updating point
 double Error=2*Tolerance;
 double MaxError=2*Tolerance;
 int Iterations=0;
 double NewValue=0.0;
 int i=0;
 int j=0;
	 extern double t;
double Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
double Pout = RetrieveDoubleParameter( ParameterFile , "Pout");
	extern double Lym;
	extern double LymPressure;
	 extern Matrix HomPressure;
	 extern double WaterPressureMin;
	 extern double PLmin;
	 extern double PLmax;
	 extern double KLmax;
	 PLmin = RetrieveDoubleParameter( ParameterFile , "PLmin");
	 PLmax = RetrieveDoubleParameter( ParameterFile , "PLmax");
	 KLmax = RetrieveDoubleParameter( ParameterFile , "KLmax");
	 extern double Hom,Hom_o;
	 Hom_o = RetrieveDoubleParameter( ParameterFile , "Hom_o");
	 Hom = RetrieveDoubleParameter( ParameterFile , "Hom");
	Lym = RetrieveDoubleParameter( ParameterFile , "Lym");
	LymPressure = RetrieveDoubleParameter( ParameterFile , "LymPressure");
	
	 WaterPressureMin = RetrieveDoubleParameter(ParameterFile, "WaterPressureMin");
	 double CondTissue = RetrieveDoubleParameter( ParameterFile , "CondTissue");
	 extern Matrix Shuffle;
	 
	 extern char SaveDirectory[1025];
	 char temp_string[1024];
	 
	 /*********generate index and save position*******/
	 
	 //sprintf( Command, "./CompositeFrame %f frame%08i.bmp", time, frame );
	 
	 
	 //sprintf( temp_string, "%s/fangneed_%f.txt", SaveDirectory,t);
	 //ofstream fang_list( temp_string, ios::out );
	 //TIC();
Matrix DiffusionConstant = ones(Xnodes,Ynodes);




  double MaxConduct= 1e-50;
  
 for(int ii=0; ii<X.TellCols(); ii++)
 {
	for(int jj=0; jj<Y.TellCols(); jj++)
	{
	//cout<<*BloodVesselIndicatorX(ii,jj)<<endl;
	if(*ConductX(ii,jj)>MaxConduct)
	{MaxConduct=*ConductX(ii,jj);}
	if(*ConductY(ii,jj)>MaxConduct)
	{MaxConduct=*ConductY(ii,jj);}
	}
 }
 
  for(int ii=0; ii<X.TellCols(); ii++)
 {
	for(int jj=0; jj<Y.TellCols(); jj++)
	{
	//cout<<*BloodVesselIndicatorX(ii,jj)<<endl;
		*DiffusionConstant(ii,jj) = CondTissue;
	}
 }
//BloodNodePressure=zeros(Xnodes,Ynodes);		
 
 while(MaxError>Tolerance&&Iterations<MaxIterations)
 {
//relocalize the point
 pCurrentVesselPoint = pFirstVesselPoint;
 MaxError=0;
  //sweep along the chain
 while(pCurrentVesselPoint)
 {
 i=pCurrentVesselPoint->i;
 j=pCurrentVesselPoint->j;
	 
/*
 if(pCurrentVesselPoint->AmIArtery)
 {
 if(i==25||j==25)
 {
 NewValue = Pin;
 }
 else if(i==75||j==75)
 {
 NewValue = Pin-250;
 }
 else if(i==125||j==125)
 {
 NewValue = Pin-500;
 }
  else if(i==175||j==175)
 {
 NewValue = Pin-750;
 }
 
 }
 else if(pCurrentVesselPoint->AmIVenous)
 {
 if(i==25)
 {
 NewValue = Pout+300;
 }
 else if(i==75)
 {
 NewValue = Pout+200;
 }
  else if(i==125)
 {
 NewValue = Pout+100;
 }
   else if(i==175)
 {
 NewValue = Pout;
 }

 }
*/
if(i==0&&j==12||j==0&&i==12)
{
NewValue = 3750;
}
else if(i==0&&j==37||j==0&&i==37)
{
NewValue = 3700;
}
else if(i==0&&j==62||j==0&&i==62)
{
NewValue = 3650;
}
else if(i==0&&j==87||j==0&&i==87)
{
NewValue = 3600;
}
else if(i==0&&j==112||j==0&&i==112)
{
NewValue = 3550;
}
else if(i==0&&j==137||j==0&&i==137)
{
NewValue = 3500;
}
else if(i==0&&j==162||j==0&&i==162)
{
NewValue = 3450;
}
else if(i==0&&j==187||j==0&&i==187)
{
NewValue = 3400;
}
else if(i==200&&j==12||j==200&&i==12)
{
NewValue = 3350;
}
else if(i==200&&j==37||j==200&&i==37)
{
NewValue = 3300;
}
else if(i==200&&j==62||j==200&&i==62)
{
NewValue = 3250;
}
else if(i==200&&j==87||j==200&&i==87)
{
NewValue = 3200;
}
else if(i==200&&j==112||j==200&&i==112)
{
NewValue = 3150;
}
else if(i==200&&j==137||j==200&&i==137)
{
NewValue = 3100;
}
else if(i==200&&j==162||j==200&&i==162)
{
NewValue = 3050;
}
else if(i==200&&j==187||j==200&&i==187)
{
NewValue = 3000;
}

/*
if(i==0&&j==12||j==0&&i==12)
{
	NewValue = 4125;//3562.5;//3750;
}
else if(i==0&&j==37||j==0&&i==37)
{
	NewValue = 4025;//3537.5;//3700;
}
else if(i==0&&j==62||j==0&&i==62)
{
	NewValue = 3925;//3512.5;//3650;
}
else if(i==0&&j==87||j==0&&i==87)
{
	NewValue = 3825;//3487.5;//3600;
}
else if(i==0&&j==112||j==0&&i==112)
{
	NewValue = 3725;//3462.5;//3550;
}
else if(i==0&&j==137||j==0&&i==137)
{
	NewValue = 3625;//3437.5;//3500;
}
else if(i==0&&j==162||j==0&&i==162)
{
	NewValue = 3535;//3412.5;//3450;
}
else if(i==0&&j==187||j==0&&i==187)
{
	NewValue = 3435;//3387.5;//3400;
}
else if(i==200&&j==12||j==200&&i==12)
{
	NewValue = 3325;//3362.5;//3350;
}
else if(i==200&&j==37||j==200&&i==37)
{
	NewValue = 3225;//3337.5;//3300;
}
else if(i==200&&j==62||j==200&&i==62)
{
	NewValue = 3125;//3312.5;//3250;
}
else if(i==200&&j==87||j==200&&i==87)
{
	NewValue = 3025;//3287.5;//3200;
}
else if(i==200&&j==112||j==200&&i==112)
{
	NewValue = 2925;//3262.5;//3150;
}
else if(i==200&&j==137||j==200&&i==137)
{
	NewValue = 2825;//3237.5;//3100;
}
else if(i==200&&j==162||j==200&&i==162)
{
	NewValue = 2725;//3212.5;//3050;
}
else if(i==200&&j==187||j==200&&i==187)
{
	NewValue = 2625;//3187.5;//3000;
}


if(i==0&&j==12||j==0&&i==12)
{
	NewValue = 3562.5;//3750;
}
else if(i==0&&j==37||j==0&&i==37)
{
	NewValue = 3537.5;//3700;
}
else if(i==0&&j==62||j==0&&i==62)
{
	NewValue = 3512.5;//3650;
}
else if(i==0&&j==87||j==0&&i==87)
{
	NewValue = 3487.5;//3600;
}
else if(i==0&&j==112||j==0&&i==112)
{
	NewValue = 3462.5;//3550;
}
else if(i==0&&j==137||j==0&&i==137)
{
	NewValue = 3437.5;//3500;
}
else if(i==0&&j==162||j==0&&i==162)
{
	NewValue = 3412.5;//3450;
}
else if(i==0&&j==187||j==0&&i==187)
{
	NewValue = 3387.5;//3400;
}
else if(i==200&&j==12||j==200&&i==12)
{
	NewValue = 3362.5;//3350;
}
else if(i==200&&j==37||j==200&&i==37)
{
	NewValue = 3337.5;//3300;
}
else if(i==200&&j==62||j==200&&i==62)
{
	NewValue = 3312.5;//3250;
}
else if(i==200&&j==87||j==200&&i==87)
{
	NewValue = 3287.5;//3200;
}
else if(i==200&&j==112||j==200&&i==112)
{
	NewValue = 3262.5;//3150;
}
else if(i==200&&j==137||j==200&&i==137)
{
	NewValue = 3237.5;//3100;
}
else if(i==200&&j==162||j==200&&i==162)
{
	NewValue = 3212.5;//3050;
}
else if(i==200&&j==187||j==200&&i==187)
{
	NewValue = 3187.5;//3000;
}
*/

 else
 {
 NewValue =AngioLymSORUpdate( Permeability, InterPressure, BloodNodePressure, ConductX, ConductY, BloodVesselIndicatorX, BloodVesselIndicatorY, i, j, MaxConduct);

 }	 
 
 Error = min_abs( NewValue - *BloodNodePressure(i,j) );
 if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 
 *BloodNodePressure(i,j) = NewValue;
 //update fluid
/* 
if(i==Xnodes-1||i==0||j==Ynodes-1||j==0)
{
NewValue = FluidPressureUpdateBoundaryPoint( DiffusionConstant,  BloodNodePressure, InterPressure, i, j);

}
else
{
 NewValue = AngioFluidPressureUpdate( DiffusionConstant,  BloodNodePressure, InterPressure,   i, j);
}
  Error = min_abs( NewValue - *InterPressure(i,j) );
  if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 *InterPressure(i, j) = NewValue;
 */
 pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//Move on
// cout << "          error     : " << MaxError <<  endl; 
 }
 
 //update fluid
 /*
 
 //boundary
  
  for( i=0 ; i <= Xnodes-1 ; i++ )
  {
   j = 0;
   NewValue = FluidPressureUpdateBoundaryPoint( DiffusionConstant,  BloodNodePressure, InterPressure, i, j);
   Error = min_abs( NewValue - *InterPressure(i,j) );
   if( Error > MaxError )
	{ MaxError = Error; }
 *InterPressure(i, j) = NewValue;
   
   j = Ynodes-1;
  NewValue = FluidPressureUpdateBoundaryPoint( DiffusionConstant,  BloodNodePressure, InterPressure, i, j);
   Error = min_abs( NewValue - *InterPressure(i,j) );
   if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 *InterPressure(i, j) = NewValue;

  }
 
  for( j=1 ; j < Ynodes-1 ; j++ )
  {
   i = 0;
   NewValue = FluidPressureUpdateBoundaryPoint( DiffusionConstant,  BloodNodePressure, InterPressure, i, j);
   Error = min_abs( NewValue - *InterPressure(i,j) );
   if( Error > MaxError )
	{ 
			MaxError = Error;
			
	}
 *InterPressure(i, j) = NewValue;

   i = Xnodes-1;
  NewValue = FluidPressureUpdateBoundaryPoint( DiffusionConstant,  BloodNodePressure, InterPressure, i, j);
   Error = min_abs( NewValue - *InterPressure(i,j) );
   if( Error > MaxError )
	{ 
			MaxError = Error;
			
	}

 *InterPressure(i, j) = NewValue;
  }

 
 //unboundary
 if( Iterations % 4 == 0 )
  {
   for( j=1 ; j < Ynodes-1 ; j++ )
   {
    for( i=1 ; i < Xnodes-1 ; i++ )
    {
	
 NewValue = AngioFluidPressureUpdate( DiffusionConstant,  BloodNodePressure, InterPressure,   i, j);
  Error = min_abs( NewValue - *InterPressure(i,j) );
 if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 *InterPressure(i, j) = NewValue;

    }
   }
  }
  
  if( Iterations % 4 == 1 )
  {
   for( j=Ynodes-2 ; j > 0 ; j-- )
   {
    for( i=1 ; i < Xnodes-1 ; i++ )
    {
      NewValue = AngioFluidPressureUpdate( DiffusionConstant,  BloodNodePressure, InterPressure,   i, j);
  Error = min_abs( NewValue - *InterPressure(i,j) );
if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 *InterPressure(i, j) = NewValue;

    }
   }
  }
  
  if( Iterations % 4 == 2 )
  {
   for( j=Ynodes-2 ; j > 0 ; j-- )
   {
    for( i=Xnodes-2 ; i > 0 ; i-- )
    {
      NewValue = AngioFluidPressureUpdate( DiffusionConstant,  BloodNodePressure, InterPressure,   i, j);
  Error = min_abs( NewValue - *InterPressure(i,j) );
 if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 *InterPressure(i, j) = NewValue;
    }
   }
  }
  
  if( Iterations % 4 == 3 )
  {
   for( j=1 ; j < Ynodes-1 ; j++ )
   {
    for( i=Xnodes-2 ; i > 0 ; i-- )
    {
      NewValue = AngioFluidPressureUpdate( DiffusionConstant,  BloodNodePressure, InterPressure,   i, j);
  Error = min_abs( NewValue - *InterPressure(i,j) );
 if( Error > MaxError )
	{ 
			MaxError = Error;
	
	}
 *InterPressure(i, j) = NewValue;

    }
   }
  }

 
 
 */
 
 
 
 
 
 
 
 Iterations++;
//cout << "iterations: " << Iterations << endl
//	<< "          error     : " << MaxError <<  endl; 

  if( Iterations % 1000 == 0 )
  { cout << Iterations << " : " << MaxError << endl; }  
 }
	/* 
	 
	 pCurrentVesselPoint = pFirstVesselPoint;

	 //sweep along the chain
	 while(pCurrentVesselPoint)
	 {
		 i=pCurrentVesselPoint->i;
		 j=pCurrentVesselPoint->j;
	 if(*BloodNodePressure(i,j)<0.001)
	 {
		 *Shuffle(i,j)=1;	 
	 }
	 
	 pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
	 }
*/
	  pCurrentVesselPoint = pFirstVesselPoint;

	 //sweep along the chain
	 while(pCurrentVesselPoint)
	 {
		 i=pCurrentVesselPoint->i;
		 j=pCurrentVesselPoint->j;
	 if(*BloodNodePressure(i,j)<0.001)
	 {
		 *HomPressure(i,j) = 0;	 
	 }
	 
	 pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
	 }

	 
	 
 cout << "summary:: iterations: " << Iterations << endl
      << "          error     : " << MaxError << endl << endl; 
 
	 //TOC();
	// fang_list<<Iterations<<endl;
	// fang_list<<VesselListLength<<endl;
	// fang_list<< StopwatchValue()<<endl;
	 //fang_list.close();
 }


 
 
double AngioLymSORUpdate( Matrix& Permeability, Matrix& InterPressure, Matrix& Solution, Matrix& ConductX, Matrix& ConductY, Matrix& BloodVesselIndicatorX, Matrix& BloodVesselIndicatorY,  int i, int j, double MaxConduct)
{
	
	extern Matrix Conduct;
	extern Matrix BloodVesselRadius;
	extern Matrix BloodVesselRadiusXdir;
	extern Matrix BloodVesselRadiusYdir;
	extern Matrix BloodVesselIndicator;
	extern Matrix HomPressure;
	extern Matrix Phi;
	extern Matrix CollapseClock;
	double dt = 0.8/MaxConduct;
	extern double t;
	extern Matrix X;
	double OutPut = 0.0;
	extern int Xnodes,Ynodes;
	// extern Matrix DirectionX,DirectionY;
	double p= *Permeability(i,j);
	
	double r= *BloodVesselRadius(i,j);
	
	extern double Hom_o, Hom;

	
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;//So, BloodVessel is also important.
	
	
	
	double Uij = *Solution(i,j);
	
	if(*Phi(i,j)<0)
	{
	*HomPressure(i,j) = Uij - Hom;
	}
	else
	{
	*HomPressure(i,j) = Uij - Hom_o;
	}
	
	double Ui_jp1 = 0;
	double Ui_jm1 = 0;
	double Uim1_j = 0;
	double Uip1_j = 0;
	
	double CoeffYell = 0;
	double CoeffYr = 0;
	double CoeffXell = 0;
	double CoeffXr   = 0; 
	double CoeffAv = 0;
	
	
	//if()
	if(min_abs(*BloodVesselIndicatorX(i,j))+min_abs(*BloodVesselIndicatorY(i,j))>0.5&& *CollapseClock(i,j)==0)
	{
		////////
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)	
		{
			if(i==0)
			{
				CoeffXell =0;
				
			}
			else
			{
				CoeffXell = *ConductX(i,j);//(*ConductX(i,j)+*ConductX(i-1,j))/2 ;
				if(*CollapseClock(i-1,j)>0)
				{
					CoeffXell   = 0 ;
				}
				
			
			}
			
			if(i==Xnodes-1)
			{
				CoeffXr   = 0 ;
			}
			else
			{
				
				CoeffXr   = *ConductX(i+1,j) ;
				if(*CollapseClock(i+1,j)>0)
				{
					CoeffXr   = 0 ;
				}
				
			}
			if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
			{
				if(j==0)
				{
					CoeffYell = 0;//(*ConductY(i,j)+*ConductY(i,j))/2 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
					CoeffYr   = *ConductY(i,j+1);
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
				}
				else if(j==Ynodes-1)
				{
					CoeffYell = *ConductY(i,j);//(*ConductY(i,j)+*ConductY(i,j))/2 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
					
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
					
					CoeffYr   = 0;
				}
				else
				{
					CoeffYell = *ConductY(i,j);
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
					CoeffYr   = *ConductY(i,j+1);
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
				}
			}
			else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
			{
				
			}
			else if(*BloodVesselIndicatorY(i,j)>0)
			{
				if(j==0)
				{
					
					CoeffYr   = *ConductY(i,j+1);
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
				}
				else if(j==Ynodes-1)
				{
					
					CoeffYr   = 0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
				}
				else
				{
					
					CoeffYr   = *ConductY(i,j+1); 
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
				}
			}
			else
			{
				if(j==0)
				{
					CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
					
				}
				else if(j==Ynodes-1)
				{
					CoeffYell = *ConductY(i,j);
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
					
				}
				else
				{
					CoeffYell =  *ConductY(i,j);	
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
				}
			}
		}
		else if(min_abs(*BloodVesselIndicatorX(i,j))<0.5)
		{
			if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
			{
				if(j==0)
				{
					CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
					CoeffYr   = *ConductY(i,j+1) ;
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
				}
				else if(j==Ynodes-1)
				{
					CoeffYell = *ConductY(i,j) ;
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
					CoeffYr   = 0 ;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
				}
				else
				{
					CoeffYell =  *ConductY(i,j) ;
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
					CoeffYr   = *ConductY(i,j+1) ;
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
				}
			}
			else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
			{
				
			}
			else if(*BloodVesselIndicatorY(i,j)>0)
			{
				if(j==0)
				{
					
					CoeffYr   = *ConductY(i,j+1);
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
				}
				else if(j==Ynodes-1)
				{
					
					CoeffYr   = 0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
				}
				else
				{
					
					CoeffYr   =*ConductY(i,j+1); 
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
				}
			}
			else
			{
				if(j==0)
				{
					CoeffYell = 0;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
					
				}
				else if(j==Ynodes-1)
				{
					CoeffYell = *ConductY(i,j);
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
					
				}
				else
				{
					CoeffYell = *ConductY(i,j);
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
					
				}
			}
		}
		else if(*BloodVesselIndicatorX(i,j)>0)
		{
			
			if(i==Xnodes-1)
			{
				CoeffXr   = 0 ;
			}
			else
			{
				CoeffXr   = *ConductX(i+1,j); 
				if(*CollapseClock(i+1,j)>0)
				{
					CoeffXr   = 0 ;
				}
				
			}
			
			if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
			{
				if(j==0)
				{
					CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
					CoeffYr   = *ConductY(i,j+1);
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
					
				}
				else if(j==Ynodes-1)
				{
					CoeffYell = *ConductY(i,j) ;
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
					CoeffYr   =0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
				}
				else
				{
					CoeffYell = *ConductY(i,j) ;
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
					CoeffYr   = *ConductY(i,j+1) ; 
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
				}
			}
			else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
			{
				
			}
			else if(*BloodVesselIndicatorY(i,j)>0)
			{
				if(j==0)
				{
					
					CoeffYr   = *ConductY(i,j+1) ;
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
				}
				else if(j==Ynodes-1)
				{
					
					CoeffYr   = 0 ;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
				}
				else
				{
					
					CoeffYr   = *ConductY(i,j+1) ; 
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
				}
			}
			else
			{
				if(j==0)
				{
					CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
					
				}
				else if(j==Ynodes-1)
				{
					CoeffYell = *ConductY(i,j) ;
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
					
				}
				else
				{
					CoeffYell = *ConductY(i,j) ;
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
					
				}
			}
		}
		else
		{
			if(i==0)
			{
				CoeffXell =0;
			}
			else
			{
				CoeffXell = *ConductX(i,j) ;
				if(*CollapseClock(i-1,j)>0)
				{
					CoeffXell   = 0 ;
				}
			}
			
			
			if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
			{
				if(j==0)
				{
					CoeffYell = 0;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
					CoeffYr   = *ConductY(i,j+1) ;
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
				}
				else if(j==Ynodes-1)
				{
					CoeffYell = *ConductY(i,j) ;
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
					CoeffYr   = 0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
				}
				else
				{
					CoeffYell = *ConductY(i,j) ;
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
					CoeffYr   = *ConductY(i,j+1) ;
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
				}
			}
			else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
			{
				
			}
			else if(*BloodVesselIndicatorY(i,j)>0)
			{
				if(j==0)
				{
					
					CoeffYr   = *ConductY(i,j+1);
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
				}
				else if(j==Ynodes-1)
				{
					
					CoeffYr   = 0 ;//(*ConductY(i,j)+*ConductY(i,0))/2 ;
				}
				else
				{
					
					CoeffYr   = *ConductY(i,j+1) ; 
					if(*CollapseClock(i,j+1)>0)
					{
						CoeffYr   = 0 ;
					}
				}
			}
			else
			{
				if(j==0)
				{
					CoeffYell = 0;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
					
				}
				else if(j==Ynodes-1)
				{
					CoeffYell = *ConductY(i,j);
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
					
				}
				else
				{
					CoeffYell = *ConductY(i,j);
					if(*CollapseClock(i,j-1)>0)
					{
						CoeffYell   = 0 ;
					}
					
				}
			}
		}
		
		
		////// 
		if(i==0)
		{
			Uim1_j = *Solution(i,j); 
		}
		else
		{
			Uim1_j = *Solution(i-1,j);  
		}
		if(i==Xnodes-1)
		{
			Uip1_j = *Solution(i,j);
		}
		else
		{
			Uip1_j = *Solution(i+1,j);
		}
		
		
		
		if(j==0)
		{
			Ui_jm1 = *Solution(i,j); //Ui_jm1 = *Solution(i,Ynodes-1);
		}
		else
		{
			Ui_jm1 = *Solution(i,j-1);  
		}
		if(j==Ynodes-1)
		{
			Ui_jp1 = *Solution(i,j);//Ui_jp1 = *Solution(i,0);
		}
		else
		{
			Ui_jp1 = *Solution(i,j+1);
		}
		
		
		//Conduct is in the unit of mm^2, firt of all, need to convert it back to m^2
		CoeffAv=*Conduct(i,j)*1e-15;// 1e-18*lambda V = 1e-18 K_{vl} S 
		
		////CoeffAv= 0;
		//if(j!=Ynodes-1)
		//{
		//  cout<<i<<","<<j<<*BloodVesselIndicacd torX(i,j)<<"  "<<*BloodVesselIndicatorY(i,j)<<endl;
		//  cout<<"CoeffXell"<<CoeffXell<<"CoeffXr"<<CoeffXr<<endl;
	
		// cout<<"CoeffYell"<<CoeffYell<<"CoeffYr"<<CoeffYr<<endl;
		double Coeffecient = (CoeffXell + CoeffXr)+ (CoeffYell + CoeffYr);
		if(Coeffecient>0)
		{
			
			//}  
			//cout<<"Conduct"<<CoeffAv<<"  "<<CoeffXell<<"  "<<CoeffXr<<"  "<<CoeffYell<<"  "<<CoeffYr<<endl;
			//cout<<" "<<Uim1_j<<" "<<Uip1_j<<" "<<Ui_jp1<<" "<<Ui_jm1<<endl;
			//getchar();
			//cout<<1.85*((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)+ p * CoeffAv * *InterPressure(i, j))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr)+ p * CoeffAv  )-0.85*Uij;
			
			//OutPut = 1.85*((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)+ p * CoeffAv * *InterPressure(i, j))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr)+  p * CoeffAv  )-0.85*Uij;
		//OutPut = 1.85*((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)+  p * CoeffAv * (*InterPressure(i, j) - *HomPressure(i,j)))/(Coeffecient)-0.85*Uij;
			//OutPut = 1.85*((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr))-0.85*Uij;
		
			if(*Phi(i,j)<0)
			{
				//OutPut = 1.85*((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)+ p * CoeffAv * (*InterPressure(i, j)+Hom))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr)+  p * CoeffAv  )-0.85*Uij;
				OutPut = ((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)+ p * CoeffAv * (*InterPressure(i, j)+Hom))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr)+  p * CoeffAv  );
			}
			else
			{
				//OutPut = 1.85*((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)+ p * CoeffAv * (*InterPressure(i, j)+Hom_o))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr)+  p * CoeffAv  )-0.85*Uij;
				OutPut = ((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)+ p * CoeffAv * (*InterPressure(i, j)+Hom_o))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr)+  p * CoeffAv  );
			}
			
			
		}
		else
		{
			
			OutPut = 0;
			//OutPut = HomPressure;
		}
		//if(!(OutPut<10000.0&&OutPut>=0.0))
		//{
		//	cout<<OutPut<<"i  "<<i<<"j   "<<j<<" Uim1_j "<< Uim1_j<<" Uip1_j "<< Uip1_j<<" Ui_jm1 "<< Ui_jm1<<" Ui_jp1 "<< Ui_jp1<<endl;
		//	cout<<" left "<< CoeffXell<<" right "<< CoeffXr<<" down "<<CoeffYell<<" up "<<CoeffYr; 
		//	getchar();
		//}	
		
		return  OutPut;
		//return ((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)+ p * CoeffAv * *InterPressure(i, j))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr)+ p * CoeffAv  );
		//return 1.85*((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr) )-0.85*Uij;   
		// return ((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)+(p * r )* *InterPressure(i, j))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr)+(p * r) );
	}
	else// single point
	{
		//return HomPressure;
		return 0;
	}
	
	
}







double AngioFluidPressureUpdate( Matrix& Diffusion,  Matrix& BloodNodePressure, Matrix& Pressure, Matrix& Solution, Matrix& CellSource,  int i, int j)
{
	
	extern Matrix BloodVesselIndicator;
	extern Matrix LymIndicator;
	extern Matrix Permeability;
	extern Matrix Conduct;
	extern double Lym;
	extern double LymPressure;
	extern Matrix HomPressure;
	extern double WaterPressureMin;
	extern Matrix Original_ECM;
	extern double VFWater;
	extern double VFCell;
	extern double ChMobility;
	extern double ChLength;
	extern double MitosisRate;
	double drain=0.0;
	extern Matrix BloodVesselRadius;
	extern Matrix CollapseClock;
	extern Matrix X;
	extern Matrix Y;
	extern double PLmin;
	extern double PLmax;
	extern double KLmax;
	extern int Xnodes,Ynodes;
	double dx = *X(1)-*X(0);
	double dy = *Y(1)-*Y(0);
	dx = 10;
	dy = 10;
	
	double dx2 = square(dx);
	double dy2 = square(dy);
	//double PTemp = (MitosisRate*ChLength*ChLength* *Pressure(i,j)/ChMobility)*(VFCell/VFWater);
	double PTemp = *Pressure(i,j);
	//double Csource = 0;
	double Csource = -(VFCell/VFWater)*MitosisRate* *CellSource(i,j);
	
	double Uij = *Solution(i,j);
	double Uim1_j = *Solution(i-1,j);
	double Uip1_j = *Solution(i+1,j);
	double Ui_jm1 = *Solution(i,j-1);
	double Ui_jp1 = *Solution(i,j+1);
	
	//double dt = 1.0;
	
	
	double CenterDiffusion = *Diffusion(i,j);
	
	double CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
	double CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
	double CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
	double CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
	
    double fv = 0.0;
	double gv = 0.0;
	double fl = 0.0;
	double gl = 0.0;
	
	
	
	
	if(*BloodVesselIndicator(i,j)>0.5)
	{
		//if(HomPressure>(*Solution(i,j)+PTemp)&& *CollapseClock(i,j)==0)
		//if(*HomPressure(i,j)> *Solution(i,j)&& *CollapseClock(i,j)==0)
		if(*CollapseClock(i,j)==0)
		{
		//f=*Permeability(i, j)* *Conduct(i, j)* *BloodNodePressure(i,j);
		 //fv=*Permeability(i, j)* *Conduct(i, j)* (HomPressure- PTemp);
			fv=*Permeability(i, j)* *Conduct(i, j)* *HomPressure(i,j);
		 gv=*Permeability(i, j)* *Conduct(i, j) ;
		}
		
	}
	
	
	
	//if(LymPressure<(*Solution(i,j)+PTemp)&&(*Solution(i,j)>WaterPressureMin))
	//if(LymPressure<(*Solution(i,j)+PTemp))
	if(LymPressure< *Solution(i,j))
	{		
		
		double KLTemp;
		if(PTemp<=PLmin)
		{
			KLTemp = 1+(KLmax-1)*PTemp/PLmin;
		}
		else if(PTemp>PLmin && PTemp<PLmax)
		{
			KLTemp = KLmax*(1-(PTemp-PLmin)/(PLmax-PLmin));
		}
		else
		{
			KLTemp = 0;	
		}

	 //fl= Lym* *Original_ECM(i,j)*(LymPressure-PTemp);
		fl= KLTemp*Lym* *Original_ECM(i,j)*LymPressure;
	 gl =KLTemp*Lym* *Original_ECM(i,j);
	}
	//cout<<"dada"<<endl;
	
	double output = ((( (CoeffXell*Uim1_j + CoeffXr*Uip1_j)/dx2
					   + (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)/dy2+ fv+fl + Csource)
					  /(  (CoeffXell+CoeffXr)/dx2 + (CoeffYell+CoeffYr)/dy2 + gv+gl )))*1.85-0.85*Uij;
	
	
	//if(output<WaterPressureMin)
	//{
	//	output = WaterPressureMin;	 
	//}
	
	return output;
	
	/*
	if(*BloodVesselIndicator(i,j)>0.5)
	{
		if(HomPressure>(*Solution(i,j)+PTemp))
		{
			//f=*Permeability(i, j)* *Conduct(i, j)* *BloodNodePressure(i,j);
			fv=*Permeability(i, j)* *Conduct(i, j)* (HomPressure/VFWater- PTemp- *Solution(i,j));
			
		}
		
	}
	
	
	
	if(LymPressure<(*Solution(i,j)+PTemp))
	{		
		fl= Lym* *ECM(i,j)*(LymPressure/VFWater-PTemp- *Solution(i,j));
		
	}
	//cout<<"dada"<<endl;
	
	return((( (CoeffXell*Uim1_j + CoeffXr*Uip1_j)/dx2
			 + (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)/dy2+ fv+fl - Csource)
			/(  (CoeffXell+CoeffXr)/dx2 + (CoeffYell+CoeffYr)/dy2 )))*1.85 - 0.85* Uij;
	*/
}

double AngioFluidPressureUpdate( Matrix& Diffusion,  Matrix& BloodNodePressure, Matrix& Solution,   int i, int j)
{

extern Matrix BloodVesselIndicator;
extern Matrix LymIndicator;
extern Matrix Permeability;
extern Matrix Conduct;
extern double Lym;
extern double LymPressure;
extern Matrix HomPressure;
extern Matrix ECM;
extern Matrix Pressure;
	extern double VFWater;
	extern double VFCell;
	extern double ChMobility;
	extern double ChLength;
	extern double MitosisRate;
 double drain=0.0;
 extern Matrix BloodVesselRadius;

 extern Matrix X;
 extern Matrix Y;
 extern int Xnodes,Ynodes;
 double dx = *X(1)-*X(0);
 double dy = *Y(1)-*Y(0);
	dx = 10;
	dy = 10;
 
 double dx2 = square(dx);
 double dy2 = square(dy);

	
 double Uij = *Solution(i,j);
 double Uim1_j = *Solution(i-1,j);
 double Uip1_j = *Solution(i+1,j);
 double Ui_jm1 = *Solution(i,j-1);
 double Ui_jp1 = *Solution(i,j+1);
 
 //double dt = 1.0;

 
 double CenterDiffusion = *Diffusion(i,j);

 double CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
 double CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
 double CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
 double CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
 
 double f=0.0;


 
 
  if(*BloodVesselIndicator(i,j)>0.5)
 {
	
	 //f=*Permeability(i, j)* *Conduct(i, j)* *BloodNodePressure(i,j);
	 f=*Permeability(i, j)* *Conduct(i, j)* *HomPressure(i,j);
	
 }
 
 f+= Lym* *ECM(i,j)*LymPressure;
 
  double g=*Permeability(i, j)* *Conduct(i, j) ;
   g+= Lym* *ECM(i,j);

//cout<<"dada"<<endl;

	return((( (CoeffXell*Uim1_j + CoeffXr*Uip1_j)/dx2
	           + (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)/dy2+ f )
	  /(  (CoeffXell+CoeffXr)/dx2 + (CoeffYell+CoeffYr)/dy2 + g )))*1.85-0.85*Uij;
	

 
}

double FluidPressureUpdateBoundaryPoint( Matrix& Diffusion,  Matrix& BloodNodePressure, Matrix& Pressure, Matrix& Solution,  int i, int j)
{
extern Matrix Conduct;
extern Matrix BloodVesselIndicator;
extern Matrix BloodVesselRadius;
extern Matrix LymIndicator;
extern Matrix Permeability;
extern Matrix Original_ECM;
	extern Matrix CollapseClock;
	extern double VFWater;
	extern double VFCell;
	extern double ChMobility;
	extern double ChLength;
	extern double PLmin;
	extern double PLmax;
	extern double KLmax;
	extern double MitosisRate;
	double PTemp = *Pressure(i,j);
	//double PTemp = (MitosisRate*ChLength*ChLength* *Pressure(i,j)/ChMobility)*(VFCell/VFWater);  
	double Csource = 0;
extern Matrix X, Y;
extern int Xnodes, Ynodes;
double dx=*X(1)-*X(0);
double dy=*Y(1)-*Y(0);
	dx = 10;
	dy = 10;
double dx2 = square(dx);
double dy2 = square(dy);
double dt = 1.0;
double drain=0.0;
double Uij = *Solution(i,j);
double CenterDiffusion = *Diffusion(i,j);
 double Uim1_j = 0;
 double Uip1_j = 0;
 double Ui_jm1 = 0;
 double Ui_jp1 = 0;
extern double Lym;
extern double LymPressure;
extern Matrix HomPressure;
extern double WaterPressureMin;

 double CoeffXell = 0;
 double CoeffXr   = 0;
 double CoeffYell = 0;
 double CoeffYr   = 0;

 if(i==0)
 {
 if(j == 0)
 {

  Uim1_j = *Solution(i+1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j+1);
  Ui_jp1 = *Solution(i,j+1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 
 }
 else if(j == Solution.TellCols() - 1)
 {
  Uim1_j = *Solution(i+1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j-1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );

 
 }
 else
 {
 
  Uim1_j = *Solution(i+1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j+1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 
 }
 }
 
 if(i==Solution.TellRows() - 1)
 {
 if(j == 0)
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i-1,j);
  Ui_jm1 = *Solution(i,j+1);
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 else if(j == Solution.TellCols() - 1)
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i-1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j-1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );

 }
 else
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i-1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 }
 
 
 if(j==0)
 {
 if(i == 0)
 {
 //done
  Uim1_j = *Solution(i+1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j+1);
  Ui_jp1 = *Solution(i,j+1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 else if(i == Solution.TellRows() - 1)
 {
 //done
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i-1,j);
  Ui_jm1 = *Solution(i,j+1);
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 else
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j+1);
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 }
 
  if(j==Solution.TellCols() - 1)
 {
 if(i == 0)
 {
 //done
  Uim1_j = *Solution(i+1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j-1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
 }
 else if(i == Solution.TellRows() - 1)
 {
 //done
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i-1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j-1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
 }
 else
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j-1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );

 }
 }

 
	double fv = 0.0;
	double gv = 0.0;
	double fl = 0.0;
	double gl = 0.0;
	
	
	

	
	
	if(*BloodVesselIndicator(i,j)>0.5)
	{
		//if(HomPressure>(*Solution(i,j)+PTemp)&& *CollapseClock(i,j)==0)
		//if(*HomPressure(i,j)> *Solution(i,j)&& *CollapseClock(i,j)==0)
		if(*CollapseClock(i,j)==0)
		{
			//f=*Permeability(i, j)* *Conduct(i, j)* *BloodNodePressure(i,j);
			//fv=*Permeability(i, j)* *Conduct(i, j)* (HomPressure- PTemp);
			fv=*Permeability(i, j)* *Conduct(i, j)* *HomPressure(i,j);
			gv=*Permeability(i, j)* *Conduct(i, j) ;
		}
		
	}
	
	
	
	//if(LymPressure<(*Solution(i,j)+PTemp)&&(*Solution(i,j)>WaterPressureMin))
	//if(LymPressure<(*Solution(i,j)+PTemp))
	if(LymPressure< *Solution(i,j))
	{		
		
		double KLTemp;
		if(PTemp<=PLmin)
		{
			KLTemp = 1+(KLmax-1)*PTemp/PLmin;
		}
		else if(PTemp>PLmin && PTemp<PLmax)
		{
			KLTemp = KLmax*(1-(PTemp-PLmin)/(PLmax-PLmin));
		}
		else
		{
			KLTemp = 0;	
		}
		
		//fl= Lym* *Original_ECM(i,j)*(LymPressure-PTemp);
		fl= KLTemp*Lym* *Original_ECM(i,j)*LymPressure;
		gl =KLTemp*Lym* *Original_ECM(i,j);
	}
	
	
	
	
	 
	 double output = ((( (CoeffXell*Uim1_j + CoeffXr*Uip1_j)/dx2
	 + (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)/dy2+ fv+fl + Csource)
	 /(  (CoeffXell+CoeffXr)/dx2 + (CoeffYell+CoeffYr)/dy2 + gv+gl )))*1.85-0.85*Uij;
	 
	
	// if(output<WaterPressureMin)
	 //{
	//	 output = WaterPressureMin;	 
	 //}
	
	return output;
	 /*
	if(*BloodVesselIndicator(i,j)>0.5)
	{
		if(HomPressure>(*Solution(i,j)+PTemp))
		{
			//f=*Permeability(i, j)* *Conduct(i, j)* *BloodNodePressure(i,j);
			fv=*Permeability(i, j)* *Conduct(i, j)* (HomPressure/VFWater - PTemp-Uij);
			
		}
		
	}
	
	
	
	if(LymPressure<(*Solution(i,j)+PTemp))
	{		
		fl= Lym* *ECM(i,j)*(LymPressure/VFWater -PTemp- Uij);
		
     }
	//cout<<"dada"<<endl;
	
	return((( (CoeffXell*Uim1_j + CoeffXr*Uip1_j)/dx2
			 + (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)/dy2+ fv+fl - Csource)
			/(  (CoeffXell+CoeffXr)/dx2 + (CoeffYell+CoeffYr)/dy2 )))*1.85 - 0.85* Uij;
	*/


}
double FluidPressureUpdateBoundaryPoint( Matrix& Diffusion,  Matrix& BloodNodePressure, Matrix& Solution,   int i, int j)
{
	extern Matrix Conduct;
	extern Matrix BloodVesselIndicator;
	extern Matrix BloodVesselRadius;
	extern Matrix LymIndicator;
	extern Matrix Permeability;
	extern Matrix ECM;
	extern Matrix X, Y;
	extern int Xnodes, Ynodes;
	double dx=*X(1)-*X(0);
	double dy=*Y(1)-*Y(0);
	dx = 10;
	dy = 10;
	double dx2 = square(dx);
	double dy2 = square(dy);
	double dt = 1.0;
	double drain=0.0;
	double Uij = *Solution(i,j);
	double CenterDiffusion = *Diffusion(i,j);
	double Uim1_j = 0;
	double Uip1_j = 0;
	double Ui_jm1 = 0;
	double Ui_jp1 = 0;
	extern double Lym;
	extern double LymPressure;
	extern Matrix HomPressure;
	extern double WaterPressureMin;
	
	double CoeffXell = 0;
	double CoeffXr   = 0;
	double CoeffYell = 0;
	double CoeffYr   = 0;
	
	if(i==0)
	{
		if(j == 0)
		{
			
			Uim1_j = *Solution(i+1,j);
			Uip1_j = *Solution(i+1,j);
			Ui_jm1 = *Solution(i,j+1);
			Ui_jp1 = *Solution(i,j+1);
			
			
			CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			
			
		}
		else if(j == Solution.TellCols() - 1)
		{
			Uim1_j = *Solution(i+1,j);
			Uip1_j = *Solution(i+1,j);
			Ui_jm1 = *Solution(i,j-1);
			Ui_jp1 = *Solution(i,j-1);
			
			
			CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			
			
		}
		else
		{
			
			Uim1_j = *Solution(i+1,j);
			Uip1_j = *Solution(i+1,j);
			Ui_jm1 = *Solution(i,j-1);
			Ui_jp1 = *Solution(i,j+1);
			
			
			CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			
			
		}
	}
	
	if(i==Solution.TellRows() - 1)
	{
		if(j == 0)
		{
			Uim1_j = *Solution(i-1,j);
			Uip1_j = *Solution(i-1,j);
			Ui_jm1 = *Solution(i,j+1);
			Ui_jp1 = *Solution(i,j+1);
			
			CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			
		}
		else if(j == Solution.TellCols() - 1)
		{
			Uim1_j = *Solution(i-1,j);
			Uip1_j = *Solution(i-1,j);
			Ui_jm1 = *Solution(i,j-1);
			Ui_jp1 = *Solution(i,j-1);
			
			CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			
		}
		else
		{
			Uim1_j = *Solution(i-1,j);
			Uip1_j = *Solution(i-1,j);
			Ui_jm1 = *Solution(i,j-1);
			Ui_jp1 = *Solution(i,j+1);
			
			CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			
		}
	}
	
	
	if(j==0)
	{
		if(i == 0)
		{
			//done
			Uim1_j = *Solution(i+1,j);
			Uip1_j = *Solution(i+1,j);
			Ui_jm1 = *Solution(i,j+1);
			Ui_jp1 = *Solution(i,j+1);
			
			
			CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			
		}
		else if(i == Solution.TellRows() - 1)
		{
			//done
			Uim1_j = *Solution(i-1,j);
			Uip1_j = *Solution(i-1,j);
			Ui_jm1 = *Solution(i,j+1);
			Ui_jp1 = *Solution(i,j+1);
			
			CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			
		}
		else
		{
			Uim1_j = *Solution(i-1,j);
			Uip1_j = *Solution(i+1,j);
			Ui_jm1 = *Solution(i,j+1);
			Ui_jp1 = *Solution(i,j+1);
			
			CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
			
		}
	}
	
	if(j==Solution.TellCols() - 1)
	{
		if(i == 0)
		{
			//done
			Uim1_j = *Solution(i+1,j);
			Uip1_j = *Solution(i+1,j);
			Ui_jm1 = *Solution(i,j-1);
			Ui_jp1 = *Solution(i,j-1);
			
			
			CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
		}
		else if(i == Solution.TellRows() - 1)
		{
			//done
			Uim1_j = *Solution(i-1,j);
			Uip1_j = *Solution(i-1,j);
			Ui_jm1 = *Solution(i,j-1);
			Ui_jp1 = *Solution(i,j-1);
			
			CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
		}
		else
		{
			Uim1_j = *Solution(i-1,j);
			Uip1_j = *Solution(i+1,j);
			Ui_jm1 = *Solution(i,j-1);
			Ui_jp1 = *Solution(i,j-1);
			
			CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
			CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
			CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
			
		}
	}
	
	
	double f=0.0;
	
	if(*BloodVesselIndicator(i,j)>0.5)
	{
		
		//f=*Permeability(i, j)* *Conduct(i, j)* *BloodNodePressure(i,j);
		f=*Permeability(i, j)* *Conduct(i, j)* *HomPressure(i,j);
		
	}
	
	f+= Lym* *ECM(i,j)*LymPressure;
	
	double g=*Permeability(i, j)* *Conduct(i, j);
	g+= Lym* *ECM(i,j);
	
	
	
	
	return (( (CoeffXell*Uim1_j + CoeffXr*Uip1_j)/dx2 
			 + (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)/dy2 + f )
			/(  (CoeffXell+CoeffXr)/dx2 + (CoeffYell+CoeffYr)/dy2 + g ))*1.85-0.85*Uij;
	
	
	
}


double FluidPressureUpdateBoundaryPointD( Matrix& Diffusion,  Matrix& BloodNodePressure, Matrix& Solution,   int i, int j)
{
extern Matrix Conduct;
extern Matrix BloodVesselIndicator;
extern Matrix BloodVesselRadius;
extern Matrix LymIndicator;
extern Matrix Permeability;
extern Matrix X, Y;
extern int Xnodes, Ynodes;
double dx=*X(1)-*X(0);
double dy=*Y(1)-*Y(0);
double dx2 = square(dx);
double dy2 = square(dy);
double dt = 1.0;
double drain=0.0;
double Uij = *Solution(i,j);
double CenterDiffusion = *Diffusion(i,j);
 double Uim1_j = 0;
 double Uip1_j = 0;
 double Ui_jm1 = 0;
 double Ui_jp1 = 0;


 double CoeffXell = 0;
 double CoeffXr   = 0;
 double CoeffYell = 0;
 double CoeffYr   = 0;

 if(i==0)
 {
 if(j == 0)
 {

  Uim1_j = 2500;
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = 2500;
  Ui_jp1 = *Solution(i,j+1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 
 }
 else if(j == Solution.TellCols() - 1)
 {
  Uim1_j = 2500;
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = 2500;


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );

 
 }
 else
 {
 
  Uim1_j = 2500;
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j+1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 
 }
 }
 
 if(i==Solution.TellRows() - 1)
 {
 if(j == 0)
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = 2500;
  Ui_jm1 = 2500;
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 else if(j == Solution.TellCols() - 1)
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = 2500;
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = 2500;
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );

 }
 else
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = 2500;
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 }
 
 
 if(j==0)
 {
 if(i == 0)
 {
 //done
  Uim1_j = 2500;
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = 2500;
  Ui_jp1 = *Solution(i,j+1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 else if(i == Solution.TellRows() - 1)
 {
 //done
  Uim1_j = *Solution(i-1,j);
  Uip1_j = 2500;
  Ui_jm1 = 2500;
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 else
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = 2500;
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 }
 
  if(j==Solution.TellCols() - 1)
 {
 if(i == 0)
 {
 //done
  Uim1_j = 2500;
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = 2500;


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
 }
 else if(i == Solution.TellRows() - 1)
 {
 //done
  Uim1_j = *Solution(i-1,j);
  Uip1_j = 2500;
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = 2500;
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
 }
 else
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = 2500;
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );

 }
 }

 
  double f=0.0;

 if(*BloodVesselIndicator(i,j)>0.5)
 {
	//f=*Permeability(i, j)* *Conduct(i, j) * *BloodNodePressure(i, j);
	 f=*Permeability(i, j)* *Conduct(i, j) * 2500.00;
	
 }
 
  double g=*Permeability(i, j)* *Conduct(i, j) ;
 if(*LymIndicator(i,j)>0.5)
 {
	g=g+drain;
 }
 

 return ((( (CoeffXell*Uim1_j + CoeffXr*Uip1_j)/dx2 
              + (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)/dy2 + f )
       /(  (CoeffXell+CoeffXr)/dx2 + (CoeffYell+CoeffYr)/dy2 + g )))*1.85-0.85*Uij;



}

void VesselStructure::GrowSprout_one_step()
{
cout <<"function GrowSprout running"<<endl; 
extern bool VesselBroke;
extern int touchpoints;
extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern Matrix BloodNodePressure;
extern Matrix VesselCells;
extern Matrix TAF;
extern Matrix ECM;
extern Matrix CellColor;
extern Matrix Shuffle;
extern int Xnodes, Ynodes;
double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
AngioDeltaY = 1.0 /Ynodes;
Matrix Prob0 = zeros(Xnodes, Ynodes);
Matrix Prob1 = zeros(Xnodes, Ynodes);
Matrix Prob2 = zeros(Xnodes, Ynodes);
Matrix Prob3 = zeros(Xnodes, Ynodes);
Matrix Prob4 = zeros(Xnodes, Ynodes);
Matrix Inf0 = zeros(Xnodes, Ynodes);
Matrix Inf1 = zeros(Xnodes, Ynodes);
Matrix Inf2 = zeros(Xnodes, Ynodes);
Matrix Inf3 = zeros(Xnodes, Ynodes);
Matrix Inf4 = zeros(Xnodes, Ynodes);
Matrix Sup0 = zeros(Xnodes, Ynodes);
Matrix Sup1 = zeros(Xnodes, Ynodes);
Matrix Sup2 = zeros(Xnodes, Ynodes);
Matrix Sup3 = zeros(Xnodes, Ynodes);
Matrix Sup4 = zeros(Xnodes, Ynodes);
int iminus,iplus,jminus,jplus;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
int LoopLength = VesselListLength; 
VesselBroke=0;
int s = 0;
 cout<<"Growth Begin"<<endl;

for(int Loop = 0; Loop < LoopLength; Loop++)
{
   //cout<<"enter loop"<<endl;
	int i = pCurrentVesselPoint1->i;
	int j = pCurrentVesselPoint1->j;
	
		if(*Shuffle(i,j)>0)
	{
		//	cout<<"beginkill";
				VesselBroke=1;
				*BloodNodePressure(i,j) = 0.0;
	//tune the indicator matrix
	    if (i == 0) {iminus=i;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=j;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = i;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = j;} else  {jplus = j+1;}
	
	  int I, J;
	  
	  I=iplus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=1;}
	  else if(*BloodVesselIndicatorX(I,J)<-0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	
	  I=iminus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=-1;}
	  else if(*BloodVesselIndicatorX(I,J)>0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
	
	 
	  I=i;
	  J=jplus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=1;}
	  else if(*BloodVesselIndicatorY(I,J)<-0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	  I=i;
	  J=jminus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=-1;}
	  else if(*BloodVesselIndicatorY(I,J)>0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
		
	  I=i;
	  J=j;
	  *BloodVesselIndicatorX(I,J)=0;
	  *BloodVesselIndicatorY(I,J)=0;	 	  
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			  
				
				  
					
	//tune the chain
					  
					  
	VesselPoint* pTempVesselPoint = pCurrentVesselPoint1;
	cout<<"i:"<<i<<",j:"<<j<<"  ";
		touchpoints++;
		cout<<"touchpoints"<<touchpoints<<"  ";

	
	
	if(pCurrentVesselPoint1->pNextVesselPoint)
	{
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=pCurrentVesselPoint1->pNextVesselPoint;
	(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	else
	{
	cout<<"the last point"<<endl;
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=NULL;
	//(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselPoint2= pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	 
	   pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
	   
	   
	  pTempVesselPoint->pPreviousVesselPoint	= NULL;
	  pTempVesselPoint->pNextVesselPoint = NULL;
	  pTempVesselPoint->i = 0;
	  pTempVesselPoint->j = 0;
	  pTempVesselPoint->AmIArtery = 0;
	  delete pTempVesselPoint;
	  //getchar();

	  
	  
	 /**
	  
	  (pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=pCurrentVesselPoint1->pNextVesselPoint;
	(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselPoint1->pPreviousVesselPoint	= NULL;
	 pCurrentVesselPoint1->pNextVesselPoint = NULL;
	cout<<"i:"<<i<<",j:"<<j<<"  ";
		touchpoints++;
		cout<<"touchpoints"<<touchpoints<<"  ";
	delete [] pCurrentVesselPoint1;
	getchar();
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
	 
	**/  



	 VesselListLength=VesselListLength-1;
	 
	 
	 if(*SproutTips(i,j)>0)
	 {
	 
	    SproutNumber=SproutNumber-*SproutTips(i,j); 
		*SproutTips(i,j)=0;
		*SproutAge(i,j)=0;
		
	 
	 }
	 else
	 {
	 *VesselAge(i,j)=0;
	 }
	 
	
	
	
	
	
	
	//cout<<"endkill";
	
	
	
	
	
	}//this is for the kill of points
	else
	{
	
	
	
if((i==0||j==0||i==Xnodes-1||j==Ynodes-1)&&*SproutTips(i,j)>0)
	{
	*SproutTips(i,j)=0;
	//SproutNumber -=1;
	}
	
	/******Modi Here*****/
	s = 0;
	if(*VesselCells(i,j)>=1)
	{
	s =  *SproutTips(i,j);
	}
	
	if(s>0)
	{
	s=1;
	}
	
	
	/*****Modi Here*****/
	for(int k=0; k< s; k++ )
	{
	
	
	//cout<<"enter sprouttip loop"<<endl;
		//cout<<"beginsprout";
	    if (i == 0) {iminus=1;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=1;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = Xnodes - 2;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = Ynodes - 2;} else  {jplus = j+1;}
		
	 		//Prob0 should be with stability 
													
	  
		  *Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i, j) );
	 
	      *Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i, j) );
	
	      *Prob3(i, j) = Chemo_Response*( *TAF(i, jplus) - *TAF(i, j) );	   
    
	      *Prob4(i, j) = Chemo_Response*( *TAF(i, jminus) - *TAF(i, j) );
		  
		   double MakeUpToZero1, MakeUpToZero2, MakeUpToZero3, MakeUpToZero4;
	  
			MakeUpToZero1 = 0;
			MakeUpToZero2 = 0;
			MakeUpToZero3 = 0;
			MakeUpToZero4 = 0;
			
		if( *Prob0(i,j) <= 0 )
		{
		*Prob0(i,j) = 0;
		}
		if( *Prob1(i,j) <= 0 )
		{
		*Prob1(i,j) = 0;
	
		}
        if( *Prob2(i,j) <= 0 )
		{
		*Prob2(i,j) = 0;
	
		}
		if( *Prob3(i,j) <= 0 )
		{
		*Prob3(i,j) = 0;
	
		}

		if( *Prob4(i,j) <= 0 )
		{
		*Prob4(i,j) = 0;
	
		}

       
	   
	   
	   
	 //the sprout shouldn't grow reverse  
	   if (min_abs(*BloodVesselIndicatorX(i,j)) > 1.5 )
	   {
	   *Prob2(i,j) = 0;
	   *Prob1(i,j) = 0;
	   
	   }
	   else if (*BloodVesselIndicatorX(i,j) > 0)
	   {
	   *Prob1(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorX(i,j) < 0)
	   {
	   *Prob2(i,j) = 0;
	   }
	   
	   if (min_abs(*BloodVesselIndicatorY(i,j)) > 1.5 )
	   {
	   *Prob3(i,j) = 0;
	   *Prob4(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) < 0 )
	   {
	   *Prob4(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) > 0 )
	   {
	   *Prob3(i,j) = 0;
	   }
	   //other exceptions 
		if (i == Xnodes-1) 
	    { *Prob1(i, j) = 0.0; }
		else if (i == 0)   
	    { *Prob2(i, j) = 0.0; }
		if (j == ( Ynodes-1))
	    { *Prob3(i, j) = 0.0; }
		 if (j == 0)
	    { *Prob4(i, j) = 0.0; }


   // cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
     //rescale the probabilities
	 double ProbTotal = *Prob0(i,j) + *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j) ;
	 if ( ProbTotal> 0 )
	 {
		*Prob0(i,j) /= ProbTotal;
		*Prob1(i,j) /= ProbTotal;
		*Prob2(i,j) /= ProbTotal;
		*Prob3(i,j) /= ProbTotal;
		*Prob4(i,j) /= ProbTotal;
	 }
	// cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
	 
	 //Get Random Intervals
	 *Inf0(i,j) = 0;
	 *Sup0(i,j) = *Inf0(i,j) + *Prob0(i,j);
	 //*Sup0(i,j) = *Inf0(i,j) + *Prob1(i,j);
	 *Inf1(i,j) = *Sup0(i,j);
	 *Sup1(i,j) = *Inf1(i,j) + *Prob1(i,j);
	 //*Sup1(i,j) = *Inf1(i,j) + *Prob0(i,j);
	 *Inf2(i,j) = *Sup1(i,j);
	 *Sup2(i,j) = *Inf2(i,j) + *Prob2(i,j);
	 *Inf3(i,j) = *Sup2(i,j);
	 *Sup3(i,j) = *Inf3(i,j) + *Prob3(i,j);
	 //*Sup3(i,j) = *Inf3(i,j) + *Prob4(i,j);
	 *Inf4(i,j) = *Sup3(i,j);   //not neccesary, delete them to speed up the program
	 *Sup4(i,j) = *Inf4(i,j) + *Prob4(i,j);
	 //*Sup4(i,j) = *Inf4(i,j) + *Prob3(i,j);
	 
	 //Done with those probabilities, begin to Move and Connect in one step.
 double RandNumber;
	 int I, J;
	 RandNumber = float( rand() ) / float( RAND_MAX);

	 if(RandNumber >=*Inf0(i,j) && RandNumber < *Sup0(i,j))
	 {
		
	 }
	 
	 else if(RandNumber >=*Inf1(i,j) && RandNumber < *Sup1(i,j))
	 {
		I = i + 1;
		J = j;
	
	
		if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
		
	     if( *SproutTips(I, J) > 0) 
		  {
			
		 *VesselCells(i,j) +=1;				
		 *VesselCells(I,J) +=1;								
		 *SproutTips(i, j) -=1;
		 *SproutTips(I, J) -=1;												
		  }
		  else
		  {
		 *SproutTips(i, j) -=1;
		 *VesselCells(I,J) +=1;
		  CellNumber+=1;
		  SproutNumber-=1;
		  }
		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		
		 	
				  
		   
			
			 
			   
		}
		else
		{
		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) -=1;
		*SproutTips(I, J) += 1;
	
		//*DirectionX(I, J) = 1;
		
		
		
		
		
		
		}
		
	
		
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
		
		
	 }
     
	 else if(RandNumber >= *Inf2(i,j) && RandNumber < *Sup2(i,j))
	 {
		I = i - 1;
		J = j;
	
		
		if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
		
	     if( *SproutTips(I, J) > 0) 
		  {
			
		 *VesselCells(i,j) +=1;				
		 *VesselCells(I,J) +=1;								
		 *SproutTips(i, j) -=1;
		 *SproutTips(I, J) -=1;												
		  }
		  else
		  {
		 *SproutTips(i, j) -=1;
		 *VesselCells(I,J) +=1;
		  CellNumber+=1;
		  SproutNumber-=1;
		  }
		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		
		 	
				  
		   
			
			 
			   
		}
		else
		{
		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) -=1;
		*SproutTips(I, J) += 1;
	
		//*DirectionX(I, J) = 1;
		
		
		
		
		
		
		}
		
		
	
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }
	 
	 else if(RandNumber >=*Inf3(i,j) && RandNumber < *Sup3(i,j))
	 {
		I = i;
		J = j + 1;
	
		if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
		
	      if( *SproutTips(I, J) > 0) 
		  {
			
		 *VesselCells(i,j) +=1;				
		 *VesselCells(I,J) +=1;								
		 *SproutTips(i, j) -=1;
		 *SproutTips(I, J) -=1;												
		  }
		  else
		  {
		 *SproutTips(i, j) -=1;
		 *VesselCells(I,J) +=1;
		  CellNumber+=1;
		  SproutNumber-=1;
		  }	  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		
		 	
				  
		   
			
			 
			   
		}
		else
		{
		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) -=1;
		*SproutTips(I, J) += 1;
	
		//*DirectionX(I, J) = 1;
		
		
		
		
		
		
		}
		

	
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }
	 
	 else if(RandNumber >= *Inf4(i,j) && RandNumber < *Sup4(i,j))
	 {
		I = i;
		J = j - 1;
	
				
		if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
		
	     if( *SproutTips(I, J) > 0) 
		  {
			
		 *VesselCells(i,j) +=1;				
		 *VesselCells(I,J) +=1;								
		 *SproutTips(i, j) -=1;
		 *SproutTips(I, J) -=1;												
		  }
		  else
		  {
		 *SproutTips(i, j) -=1;
		 *VesselCells(I,J) +=1;
		  CellNumber+=1;
		  SproutNumber-=1;
		  }		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		
				  
		   
			
			 
			   
		}
		else
		{
		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) -=1;
		*SproutTips(I, J) += 1;
	
		//*DirectionX(I, J) = 1;
		
		
		
		
		
		
		}
		
	
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }

	//BOUNDARY SPROUTS We can ask it to be periodical?
    }//this is for the if sprout
	
	}//this is for not shuffle		
	
	pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	 //cout<<"aftermove";
    }//this is for the loop
   pLastVesselPoint = pCurrentVesselPoint2;
    QuickPlot();// COMBINE
   cout <<"function GrowSprout finished"<<endl; 
 }
	
 
 double min_prob(double a, int b, int N)
 {
 double senseTAF = 0.5;
 double senseVolume = 1;

 double p = exp(senseTAF*a/MaxTAF)/exp(senseVolume*(N-b));
 /*
 if(p>1)
 {
 p = 1;
 }
 */
 if(b<N)
 {
 p = 0;
 }
 else
 {
 p = 1;
 }
 
  return p;
 }

void VesselStructure::GrowSprout(Matrix& Shuffle)
{
cout <<"function GrowSprout running"<<endl; 
extern bool VesselBroke;
extern int touchpoints;
extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern Matrix BloodNodePressure;
extern Matrix TAF;
extern Matrix ECM;
extern int Xnodes, Ynodes;
double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
//sweep the list to update rather than matrix?
//yes!
//should we have more list?
//we don't need to, because even if we have two main vessel,
//it's like a whole system with the general BC and IC.

//DON'T FORGET THE INDICATOR
AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
AngioDeltaY = 1.0 /Ynodes;
Matrix Prob0 = zeros(Xnodes, Ynodes);
Matrix Prob1 = zeros(Xnodes, Ynodes);
Matrix Prob2 = zeros(Xnodes, Ynodes);
Matrix Prob3 = zeros(Xnodes, Ynodes);
Matrix Prob4 = zeros(Xnodes, Ynodes);
Matrix Inf0 = zeros(Xnodes, Ynodes);
Matrix Inf1 = zeros(Xnodes, Ynodes);
Matrix Inf2 = zeros(Xnodes, Ynodes);
Matrix Inf3 = zeros(Xnodes, Ynodes);
Matrix Inf4 = zeros(Xnodes, Ynodes);
Matrix Sup0 = zeros(Xnodes, Ynodes);
Matrix Sup1 = zeros(Xnodes, Ynodes);
Matrix Sup2 = zeros(Xnodes, Ynodes);
Matrix Sup3 = zeros(Xnodes, Ynodes);
Matrix Sup4 = zeros(Xnodes, Ynodes);
int iminus,iplus,jminus,jplus;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list

int LoopLength = VesselListLength; 
VesselBroke=0;
cout<<"Growth Begin"<<endl;
for(int Loop = 0; Loop < LoopLength; Loop++)
{
   //cout<<"enter loop"<<endl;
	int i = pCurrentVesselPoint1->i;
	int j = pCurrentVesselPoint1->j;
	
 // cout<<i<<"    "<<j<<endl;
	//if(*Shuffle(i,j)>0&&pCurrentVesselPoint1->AmIArtery==0)
	if((*Shuffle(i,j)>0&&*VesselAge(i,j)>=VesselAgeInf))
	{
		//	cout<<"beginkill";
				VesselBroke=1;
	//tune the indicator matrix
	    if (i == 0) {iminus=i;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=j;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = i;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = j;} else  {jplus = j+1;}
	
	  int I, J;
	  
	  I=iplus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=1;}
	  else if(*BloodVesselIndicatorX(I,J)<-0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	
	  I=iminus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=-1;}
	  else if(*BloodVesselIndicatorX(I,J)>0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
	
	 
	  I=i;
	  J=jplus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=1;}
	  else if(*BloodVesselIndicatorY(I,J)<-0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	  I=i;
	  J=jminus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=-1;}
	  else if(*BloodVesselIndicatorY(I,J)>0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
		
	  I=i;
	  J=j;
	  *BloodVesselIndicatorX(I,J)=0;
	  *BloodVesselIndicatorY(I,J)=0;	 	  
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			  
				
				  
					
	//tune the chain
					  
					  
	VesselPoint* pTempVesselPoint = pCurrentVesselPoint1;
	cout<<"i:"<<i<<",j:"<<j<<"  ";
		touchpoints++;
		cout<<"touchpoints"<<touchpoints<<"  ";

	
	
	if(pCurrentVesselPoint1->pNextVesselPoint)
	{
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=pCurrentVesselPoint1->pNextVesselPoint;
	(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	else
	{
	cout<<"the last point"<<endl;
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=NULL;
	//(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselPoint2= pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	 
	   pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
	   
	   
	  pTempVesselPoint->pPreviousVesselPoint	= NULL;
	  pTempVesselPoint->pNextVesselPoint = NULL;
	  pTempVesselPoint->i = 0;
	  pTempVesselPoint->j = 0;
	  pTempVesselPoint->AmIArtery = 0;
	  delete pTempVesselPoint;
	  //getchar();

	  
	  
	 /**
	  
	  (pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=pCurrentVesselPoint1->pNextVesselPoint;
	(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselPoint1->pPreviousVesselPoint	= NULL;
	 pCurrentVesselPoint1->pNextVesselPoint = NULL;
	cout<<"i:"<<i<<",j:"<<j<<"  ";
		touchpoints++;
		cout<<"touchpoints"<<touchpoints<<"  ";
	delete [] pCurrentVesselPoint1;
	getchar();
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
	 
	**/  



	 VesselListLength=VesselListLength-1;
	 
	 
	 if(*SproutTips(i,j)>0)
	 {
		*SproutTips(i,j)=0;
		*SproutAge(i,j)=0;
		SproutNumber=SproutNumber-1;
	 
	 }
	 else
	 {
	 *VesselAge(i,j)=0;
	 }
	 
	
	
	
	
	
	
	//cout<<"endkill";
	
	
	
	
	
	}//this is for the kill of points
	else
	{
    
	if(i==0&&*SproutTips(i,j)==1)
	{
	*SproutTips(i,j)=0;
	SproutNumber -=1;
	}
	else if(i==Xnodes-1&&*SproutTips(i,j)==1)
	{
	*SproutTips(i,j)=0;
	SproutNumber -=1;
	}
	
	if( *SproutTips(i,j) > 0)
	{
	//cout<<"enter sprouttip loop"<<endl;
		//cout<<"beginsprout";
	    if (i == 0) {iminus=1;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=1;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = Xnodes - 2;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = Ynodes - 2;} else  {jplus = j+1;}
		
	       /****  			 
		*Prob0(i, j) = 1.0
	                 - 2.0 * Dif_ECM * AngioDeltaT * ( (1.0/pow(AngioDeltaX,2) ) + (1.0/pow(AngioDeltaY,2) ) )
					 + ( ( Chemo_Response * Chemo_Sense * AngioDeltaT ) / pow( ( 1.0 + Chemo_Sense * *TAF(i,j) ), 2) ) 
						* ( (pow( ( *TAF(iplus, j) - *TAF(iminus, j) ), 2) / pow( (2.0*AngioDeltaX), 2) )
	                       +(pow( ( *TAF(i, jplus) - *TAF(i, jminus) ), 2) / pow( (2.0*AngioDeltaY), 2) ) ) 
                     - ( (Chemo_Response * AngioDeltaT) / ( 1.0 + Chemo_Sense* *TAF(i, j) ) )
	                    *( ( ( *TAF(iplus, j) - 2.0 * *TAF(i, j) + *TAF(iminus, j) ) / pow(AngioDeltaX, 2) )
                          +( ( *TAF(i, jplus) - 2.0 * *TAF(i, j) + *TAF(i, jminus) ) / pow(AngioDeltaY, 2) ) )
	                 - Haptotactic_Reponse * AngioDeltaT * ( ( ( *ECM(iplus, j) - 2.0 * *ECM(i, j) + *ECM(iminus, j) ) / pow(AngioDeltaX, 2) )
	                                         +( ( *ECM(i, jplus) - 2.0 * *ECM(i, j) + *ECM(i, jminus) ) / pow(AngioDeltaY, 2) ) );
											 
	  
		  *Prob1(i, j) = ( Dif_ECM * AngioDeltaT ) / pow( AngioDeltaX, 2 )
						+ ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(iplus, j) - *TAF(i, j) )
		                      + Haptotactic_Reponse * ( *ECM(iplus, j) - *ECM(i, j) ) );
		
		


	 
	
	     *Prob2(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaX, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, j) - *TAF(iminus, j) )
		                      + Haptotactic_Reponse * ( *ECM(i, j) - *ECM(iminus, j) ) );
	
	
	
	     *Prob3(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       + ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, jplus) - *TAF(i, j) )
		                      + Haptotactic_Reponse * ( *ECM(i, jplus) - *ECM(i, j) ) );
	
	   
    
	     *Prob4(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, j) - *TAF(i, jminus) )
		                      + Haptotactic_Reponse * ( *ECM(i, j) - *ECM(i, jminus) ) );
		  *********/
		 
		*Prob0(i, j) = 1.0
	                 - 2.0 * Dif_ECM * AngioDeltaT * ( (1.0/pow(AngioDeltaX,2) ) + (1.0/pow(AngioDeltaY,2) ) )
					 + ( ( Chemo_Response * Chemo_Sense * AngioDeltaT ) / pow( ( 1.0 + Chemo_Sense * *TAF(i,j) ), 2) ) 
						* ( (pow( ( *TAF(iplus, j) - *TAF(iminus, j) ), 2) / pow( (2.0*AngioDeltaX), 2) )
	                       +(pow( ( *TAF(i, jplus) - *TAF(i, jminus) ), 2) / pow( (2.0*AngioDeltaY), 2) ) ) 
                     - ( (Chemo_Response * AngioDeltaT) / ( 1.0 + Chemo_Sense* *TAF(i, j) ) )
	                    *( ( ( *TAF(iplus, j) - 2.0 * *TAF(i, j) + *TAF(iminus, j) ) / pow(AngioDeltaX, 2) )
                          +( ( *TAF(i, jplus) - 2.0 * *TAF(i, j) + *TAF(i, jminus) ) / pow(AngioDeltaY, 2) ) )
	                 - Haptotactic_Reponse * AngioDeltaT * ( ( ( *ECM(iplus, j) - 2.0 * *ECM(i, j) + *ECM(iminus, j) ) / pow(AngioDeltaX, 2) )
	                                         +( ( *ECM(i, jplus) - 2.0 * *ECM(i, j) + *ECM(i, jminus) ) / pow(AngioDeltaY, 2) ) );
											 
	  
		  *Prob1(i, j) = ( Dif_ECM * AngioDeltaT ) / pow( AngioDeltaX, 2 )
						+ ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(iplus, j) - *TAF(iminus, j) )
		                      + Haptotactic_Reponse * ( *ECM(iplus, j) - *ECM(iminus, j) ) );
		
		


	 
	
	     *Prob2(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaX, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(iplus, j) - *TAF(iminus, j) )
		                      + Haptotactic_Reponse * ( *ECM(iplus, j) - *ECM(iminus, j) ) );
	
	
	
	     *Prob3(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       + ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, jplus) - *TAF(i, jminus) )
		                      + Haptotactic_Reponse * ( *ECM(i, jplus) - *ECM(i, jminus) ) );
	
	   
    
	     *Prob4(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, jplus) - *TAF(i, jminus) )
		                      + Haptotactic_Reponse * ( *ECM(i, jplus) - *ECM(i, jminus) ) );
							  
							

   //cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
	 // make probabilities above 0  
	   double MakeUpToZero1, MakeUpToZero2, MakeUpToZero3, MakeUpToZero4;
	  
			MakeUpToZero1 = 0;
			MakeUpToZero2 = 0;
			MakeUpToZero3 = 0;
			MakeUpToZero4 = 0;
			
		if( *Prob0(i,j) < 0 )
		{
		*Prob0(i,j) = 0;
		}
		if( *Prob1(i,j) < 0 )
		{
		MakeUpToZero1 = min_abs(*Prob1(i,j));
	
		}
		if( *Prob2(i,j) < 0 )
		{
		MakeUpToZero2 = min_abs(*Prob2(i,j));
		
		}
		if( *Prob3(i,j) < 0 )
		{
		MakeUpToZero3 = min_abs(*Prob3(i,j));
		
		}
		if( *Prob4(i,j) < 0 )
		{
		MakeUpToZero4 = min_abs(*Prob4(i,j));
		
		}
       
	   *Prob1(i,j) += (MakeUpToZero1 + MakeUpToZero2);
	   *Prob2(i,j) += (MakeUpToZero1 + MakeUpToZero2);
	   *Prob3(i,j) += (MakeUpToZero3 + MakeUpToZero4);
	   *Prob4(i,j) += (MakeUpToZero3 + MakeUpToZero4);
	   
	   
	 //the sprout shouldn't grow reverse  
	   if (*BloodVesselIndicatorX(i,j) < 0 )
	   {
	   *Prob2(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorX(i,j) > 0)
	   {
	   *Prob1(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) < 0 )
	   {
	   *Prob4(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) > 0 )
	   {
	   *Prob3(i,j) = 0;
	   }
	   //other exceptions 
		if (i == Xnodes-1) 
	    { *Prob1(i, j) = 0.0; }
		else if (i == 0)   
	    { *Prob2(i, j) = 0.0; }
		if (j == ( Ynodes-1))
	    { *Prob3(i, j) = 0.0; }
		 if (j == 0)
	    { *Prob4(i, j) = 0.0; }


   // cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
     //rescale the probabilities
	 double ProbTotal = *Prob0(i,j) + *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j) ;
	 if ( ProbTotal> 0 )
	 {
		*Prob0(i,j) /= ProbTotal;
		*Prob1(i,j) /= ProbTotal;
		*Prob2(i,j) /= ProbTotal;
		*Prob3(i,j) /= ProbTotal;
		*Prob4(i,j) /= ProbTotal;
	 }
	// cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
	 
	 //Get Random Intervals
	 *Inf0(i,j) = 0;
	 *Sup0(i,j) = *Inf0(i,j) + *Prob0(i,j);
	 //*Sup0(i,j) = *Inf0(i,j) + *Prob1(i,j);
	 *Inf1(i,j) = *Sup0(i,j);
	 *Sup1(i,j) = *Inf1(i,j) + *Prob1(i,j);
	 //*Sup1(i,j) = *Inf1(i,j) + *Prob0(i,j);
	 *Inf2(i,j) = *Sup1(i,j);
	 *Sup2(i,j) = *Inf2(i,j) + *Prob2(i,j);
	 *Inf3(i,j) = *Sup2(i,j);
	 *Sup3(i,j) = *Inf3(i,j) + *Prob3(i,j);
	 //*Sup3(i,j) = *Inf3(i,j) + *Prob4(i,j);
	 *Inf4(i,j) = *Sup3(i,j);   //not neccesary, delete them to speed up the program
	 *Sup4(i,j) = *Inf4(i,j) + *Prob4(i,j);
	 //*Sup4(i,j) = *Inf4(i,j) + *Prob3(i,j);
	 
	 //Done with those probabilities, begin to Move and Connect in one step.
	 
	 double RandNumber;
	 int I, J;
	 RandNumber = float( rand() ) / float( RAND_MAX);
	 if(RandNumber >=*Inf0(i,j) && RandNumber < *Sup0(i,j))
	 {
		I = i;
		J = j;
		*SproutAge(I, J) += AngioDeltaT;
	 }
	 
	 else if(RandNumber >=*Inf1(i,j) && RandNumber < *Sup1(i,j))
	 {
		I = i + 1;
		J = j;
		//if(*Shuffle(I,J)<0.5)
		{
		
		if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
	     if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
			SproutNumber -= 1;//comment this we have anasmosis
			
		  }
		  
		  		  
		 *SproutTips(i, j) = 0;
		 //*DirectionX(I, J) = 1;
		 *SproutTips(I, J) = 0;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	 
		}
		else
		{
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionX(I, J) = 1;
		}
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
		}
		
	 }
     
	 else if(RandNumber >= *Inf2(i,j) && RandNumber < *Sup2(i,j))
	 {
		I = i - 1;
		J = j;
		//if(*Shuffle(I,J)<0.5)
		{
		
		if(  (min_abs(*BloodVesselIndicator(I, J))) > 0 )
		{
		
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
			SproutNumber -= 1;	
			
	
		  }
		 
		  		  
		 *SproutTips(i, j) = 0;
		// *DirectionX(I, J) = -1;
		 *SproutTips(I, J) = 0;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	
		 //SproutNumber -= 1;
	 
		}
		else
		{
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob2 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionX(I, J) = -1;
		}
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		}
	 }
	 
	 else if(RandNumber >=*Inf3(i,j) && RandNumber < *Sup3(i,j))
	 {
		I = i;
		J = j + 1;
		//if(*Shuffle(I,J)<0.5)
		{
		
		if( (*BloodVesselIndicator(I, J))> 0)
		{
		 
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
				SproutNumber -= 1;
				  

		  }
		 
			  
		 *SproutTips(i, j) = 0;
		// SproutNumber -= 1;
		// *DirectionY(I, J) = 1;
		 *SproutTips(I, J) = 0;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	 
		}
		else
		{
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob3 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionY(I, J) = 1;
		}
		
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		}
	 }
	 
	 else if(RandNumber >= *Inf4(i,j) && RandNumber < *Sup4(i,j))
	 {
		I = i;
		J = j - 1;
		//if(*Shuffle(I,J)<0.5)
		{
				
		if( (*BloodVesselIndicator(I, J)) > 0)
		{
		 
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
			 SproutNumber -= 1;
		  }
		 		  
		 *SproutTips(i, j) = 0;
		// *DirectionY(I, J) = -1;
		 *SproutTips(I, J) = 0;
		 //SproutNumber -= 1;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	
		}
		else
		{		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob4 make Vessel Length"<<VesselListLength<<endl;

		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionY(I, J) = -1;
		}
		
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		}
	 }

	//BOUNDARY SPROUTS We can ask it to be periodical?
	
	 if(I == 0 || I == Xnodes - 1 || J == 0 || J == Ynodes - 1)
	 {
	  *SproutTips(I, J) = 0;
	  *SproutAge(I, J) = 0;
	 }
	 
	  // cout<<"quit sprouttip loop"<<endl;
	//cout<<"finishsprout";
	 
	 }//this is for the if
	 else
	 {
	//	cout<<"Donothing";
		*VesselAge(i,j) += AngioDeltaT; 
		//cout<<"Age"<<*VesselAge(i,j);
	 }
	 
	 }//this is for the growth of sprout
	 //cout<<"beginmove";
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	 //cout<<"aftermove";
    }//this is for the loop
   pLastVesselPoint = pCurrentVesselPoint2;
   QuickPlot();// COMBINE
   cout <<"function GrowSprout finished"<<endl; 
 }


void VesselStructure::GrowSprout_onecell(CellVesselStructure* TheVessel)
{
cout <<"function GrowSprout running"<<endl; 
extern bool VesselBroke;
extern int touchpoints;
extern Matrix SproutTips;
extern Matrix Stability;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
double Stable_down = RetrieveDoubleParameter( ParameterFile , "Stable_down");
extern Matrix TAF;
extern Matrix ECM;
extern Matrix CellColor;
extern int Xnodes, Ynodes;
double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
double String = RetrieveDoubleParameter( ParameterFile , "String");
double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
AngioDeltaY = 1.0 /Ynodes;
Matrix Prob0 = zeros(Xnodes, Ynodes);
Matrix Prob1 = zeros(Xnodes, Ynodes);
Matrix Prob2 = zeros(Xnodes, Ynodes);
Matrix Prob3 = zeros(Xnodes, Ynodes);
Matrix Prob4 = zeros(Xnodes, Ynodes);
Matrix Inf0 = zeros(Xnodes, Ynodes);
Matrix Inf1 = zeros(Xnodes, Ynodes);
Matrix Inf2 = zeros(Xnodes, Ynodes);
Matrix Inf3 = zeros(Xnodes, Ynodes);
Matrix Inf4 = zeros(Xnodes, Ynodes);
Matrix Sup0 = zeros(Xnodes, Ynodes);
Matrix Sup1 = zeros(Xnodes, Ynodes);
Matrix Sup2 = zeros(Xnodes, Ynodes);
Matrix Sup3 = zeros(Xnodes, Ynodes);
Matrix Sup4 = zeros(Xnodes, Ynodes);
int iminus,iplus,jminus,jplus;
int growstill=0;
bool setorigin=0;
int LoopLength = VesselListLength; 
 cout<<"Growth Begin"<<endl;
 VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
VesselCell* pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
VesselCell* pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
VesselCell* pTempVesselCell = TheVessel->pFirstVesselCell;

while(growstill<3)
{

cout<<"Once Begin"<<endl;
growstill++;


 Prob0 = zeros(Xnodes, Ynodes);
 Prob1 = zeros(Xnodes, Ynodes);
 Prob2 = zeros(Xnodes, Ynodes);
 Prob3 = zeros(Xnodes, Ynodes);
 Prob4 = zeros(Xnodes, Ynodes);
 Inf0 = zeros(Xnodes, Ynodes);
 Inf1 = zeros(Xnodes, Ynodes);
 Inf2 = zeros(Xnodes, Ynodes);
 Inf3 = zeros(Xnodes, Ynodes);
 Inf4 = zeros(Xnodes, Ynodes);
 Sup0 = zeros(Xnodes, Ynodes);
 Sup1 = zeros(Xnodes, Ynodes);
 Sup2 = zeros(Xnodes, Ynodes);
 Sup3 = zeros(Xnodes, Ynodes);
 Sup4 = zeros(Xnodes, Ynodes);


pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
pTempVesselCell = TheVessel->pFirstVesselCell;
LoopLength = VesselListLength;
VesselBroke=0;


for(int Loop = 0; Loop < LoopLength; Loop++)
{
   //cout<<"enter loop"<<endl;
	int i = pCurrentVesselPoint1->i;
	int j = pCurrentVesselPoint1->j;
	int i_origin = pCurrentVesselPoint1->i_origin;
	int j_origin = pCurrentVesselPoint1->j_origin;

	
	
if((i==0||j==0||i==Xnodes-1||j==Ynodes-1)&&*SproutTips(i,j)==1)
	{
	*SproutTips(i,j)=0;
	//SproutNumber -=1;
	}
	
	
	if( *SproutTips(i,j) > 0)
	{
	
	
	//cout<<"enter sprouttip loop"<<endl;
		//cout<<"beginsprout";
	    if (i == 0) {iminus=1;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=1;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = Xnodes - 2;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = Ynodes - 2;} else  {jplus = j+1;}
		
	 		//Prob0 should be with stability 
													
	  /*
		  *Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i, j) );
	 
	      *Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i, j) );
	
	      *Prob3(i, j) = Chemo_Response*( *TAF(i, jplus) - *TAF(i, j) );	   
    
	      *Prob4(i, j) = Chemo_Response*( *TAF(i, jminus) - *TAF(i, j) );
		  
	 */
	 
		*Prob0(i, j) = 0;
		*Prob1(i, j) = 0;
		*Prob2(i, j) = 0;
		*Prob3(i, j) = 0;
		*Prob4(i, j) = 0;
		if( *TAF(iplus, j) - *TAF(i, j) > 0.0)
		{
		  *Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i_origin, j_origin) ) - String*pow((iplus-i_origin)*(iplus-i_origin)+(j-j_origin)*(j-j_origin), 0.5);
	  // cout<< "*Prob1(i, j)"<<*Prob1(i, j)<<endl;
		}
		if(*TAF(iminus, j) - *TAF(i, j) > 0.0)
		{
	      *Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i_origin, j_origin) ) - String*pow((iminus-i_origin)*(iminus-i_origin)+(j-j_origin)*(j-j_origin), 0.5);
	//	  cout<< "*Prob2(i, j)"<<*Prob2(i, j)<<endl;
		}
		if(*TAF(i, jplus) - *TAF(i, j) > 0.0)
		{
	//	cout<< "*Prob3(i, j)"<<*Prob3(i, j)<<endl;
	      *Prob3(i, j) = Chemo_Response*( *TAF(i,jplus) - *TAF(i_origin, j_origin) )  - String*pow((i-i_origin)*(i-i_origin)+(jplus-j_origin)*(jplus-j_origin), 0.5);	   
		}
		if(*TAF(i, jminus) - *TAF(i, j) > 0.0)
		{
		
	//	cout<< "*Prob4(i, j)"<<*Prob4(i, j)<<endl;
	      *Prob4(i, j) = Chemo_Response*(  *TAF(i,jminus) - *TAF(i_origin, j_origin) ) - String*pow((i-i_origin)*(i-i_origin)+(jminus-j_origin)*(jminus-j_origin), 0.5);
	    }
		  
		  
		  
		
			
		if( *Prob0(i,j) <= 0 )
		{
		*Prob0(i,j) = 0;
		}
		if( *Prob1(i,j) <= 0 )
		{
		*Prob1(i,j) = 0;
	
		}
        if( *Prob2(i,j) <= 0 )
		{
		*Prob2(i,j) = 0;
	
		}
		if( *Prob3(i,j) <= 0 )
		{
		*Prob3(i,j) = 0;
	
		}

		if( *Prob4(i,j) <= 0 )
		{
		*Prob4(i,j) = 0;
	
		}

       
	   
	   
	   
	 //the sprout shouldn't grow reverse  
	   if (*BloodVesselIndicatorX(i,j) < 0 )
	   {
	   *Prob2(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorX(i,j) > 0)
	   {
	   *Prob1(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) < 0 )
	   {
	   *Prob4(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) > 0 )
	   {
	   *Prob3(i,j) = 0;
	   }
	   //other exceptions 
		if (i == Xnodes-1) 
	    { *Prob1(i, j) = 0.0; }
		else if (i == 0)   
	    { *Prob2(i, j) = 0.0; }
		if (j == ( Ynodes-1))
	    { *Prob3(i, j) = 0.0; }
		 if (j == 0)
	    { *Prob4(i, j) = 0.0; }


   // cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
     //rescale the probabilities
	 double ProbTotal = *Prob0(i,j) + *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j) ;
	 if ( ProbTotal> 0 )
	 {
		//growstill = 1;
		
		*Prob0(i,j) /= ProbTotal;
		*Prob1(i,j) /= ProbTotal;
		*Prob2(i,j) /= ProbTotal;
		*Prob3(i,j) /= ProbTotal;
		*Prob4(i,j) /= ProbTotal;
	 
	// cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
	 
	 //Get Random Intervals
	 *Inf0(i,j) = 0;
	 *Sup0(i,j) = *Inf0(i,j) + *Prob0(i,j);
	 //*Sup0(i,j) = *Inf0(i,j) + *Prob1(i,j);
	 *Inf1(i,j) = *Sup0(i,j);
	 *Sup1(i,j) = *Inf1(i,j) + *Prob1(i,j);
	 //*Sup1(i,j) = *Inf1(i,j) + *Prob0(i,j);
	 *Inf2(i,j) = *Sup1(i,j);
	 *Sup2(i,j) = *Inf2(i,j) + *Prob2(i,j);
	 *Inf3(i,j) = *Sup2(i,j);
	 *Sup3(i,j) = *Inf3(i,j) + *Prob3(i,j);
	 //*Sup3(i,j) = *Inf3(i,j) + *Prob4(i,j);
	 *Inf4(i,j) = *Sup3(i,j);   //not neccesary, delete them to speed up the program
	 *Sup4(i,j) = *Inf4(i,j) + *Prob4(i,j);
	 //*Sup4(i,j) = *Inf4(i,j) + *Prob3(i,j);
	 
	 //Done with those probabilities, begin to Move and Connect in one step.
 double RandNumber;
	 int I, J;
	 RandNumber = float( rand() ) / float( RAND_MAX);

	 if(RandNumber >=*Inf0(i,j) && RandNumber < *Sup0(i,j))
	 {
		
	 }
	 
	 else if(RandNumber >=*Inf1(i,j) && RandNumber < *Sup1(i,j))
	 {
		I = i + 1;
		J = j;
	
	
		if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
		
		
		if(*Stability(I,J)>0)
		{
           *Stability(I,J) = *Stability(I,J) - AngioDeltaT * Stable_down;
		   if(*Stability(I,J) < 0)
          {
			*Stability(I,J) = 0;
		  }
		  /****/
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
			SproutNumber -= 1;//comment this we have anasmosis
			
		  }
		  
		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;
		 *SproutTips(i, j) = 0;	
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 Connect(pCurrentVesselCell1,pTempVesselCell);
		/***/
		  
		
		}
		else
		{
	     if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
			SproutNumber -= 1;//comment this we have anasmosis
			
		  }
		  
		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;
		 *SproutTips(i, j) = 0;	
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 Connect(pCurrentVesselCell1,pTempVesselCell);
		 
		} 
		   
			
			 
			   
		}
		else
		{
		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->i_origin = i_origin;
		pCurrentVesselPoint2->j_origin = j_origin;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionX(I, J) = 1;
		
		pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		
		double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
		double sezi;
		sezi= float( rand() ) / float( RAND_MAX);
		if(sezi<Prob)
		{
		
		TheVessel->CellListLength++;
		VesselCell* pNewVesselCell = new VesselCell;
	    pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
	    pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
		pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
		pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
		pCurrentVesselCell2->cCover = 1;
		pCurrentVesselCell2->i[0] = I;
		pCurrentVesselCell2->j[0] = J;
		pCurrentVesselCell2->pNextVesselCell = NULL;
		//pCurrentVesselCell2->cConnection = 1;
	    //pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
		Connect(pCurrentVesselCell2,pCurrentVesselCell1);
		*CellColor(I,J) = 100;
			
		
		}//mitosis and grow
		else
		{
		
		pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
		pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
		pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
		
		*CellColor(I,J) = 100;
		//if(pCurrentVesselCell1->cCover==pCurrentVesselCell1->coverlimit)
		//{
		//*SproutTips(I,J)=0;
		//}
		
		}//justgrow
		
		
		
		
		
		}
		
	
		
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
		
		
	 }
     
	 else if(RandNumber >= *Inf2(i,j) && RandNumber < *Sup2(i,j))
	 {
		I = i - 1;
		J = j;
	
		
		if(  (min_abs(*BloodVesselIndicator(I, J))) > 0 )
		{
		
		
		if(*Stability(I,J)>0)
		{
           *Stability(I,J) = *Stability(I,J) - AngioDeltaT * Stable_down;
		   if(*Stability(I,J) < 0)
          {
			*Stability(I,J) = 0;
		  }
		  
		   /****/
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
			SproutNumber -= 1;//comment this we have anasmosis
			
		  }
		  
		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;
		 *SproutTips(i, j) = 0;	
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 Connect(pCurrentVesselCell1,pTempVesselCell);
		/***/
		  
		
		}
		else
		{
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
			SproutNumber -= 1;	
			
	
		  }
		 
		  		  
		// *DirectionX(I, J) = -1;
		// *SproutTips(I, J) = 1;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	
		 //SproutNumber -= 1;
		 *SproutTips(i, j) = 0;
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 Connect(pCurrentVesselCell1,pTempVesselCell);
	      }
	 
		}
		else
		{
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->i_origin = i_origin;
		pCurrentVesselPoint2->j_origin = j_origin;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob2 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		//*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionX(I, J) = -1;
		
		
		pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		
		double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
		double sezi;
		sezi= float( rand() ) / float( RAND_MAX);
		if(sezi<Prob)
		{
		
		TheVessel->CellListLength++;
		VesselCell* pNewVesselCell = new VesselCell;
	    pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
	    pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
		pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
		pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
		pCurrentVesselCell2->cCover = 1;
		pCurrentVesselCell2->i[0] = I;
		pCurrentVesselCell2->j[0] = J;
		pCurrentVesselCell2->pNextVesselCell = NULL;
		//pCurrentVesselCell2->cConnection = 1;
	    //pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
		Connect(pCurrentVesselCell2,pCurrentVesselCell1);
		*CellColor(I,J) = 100;
		
		}//mitosis and grow
		else
		{
		
		pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
		pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
		pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
		
		*CellColor(I,J) = 100;
		//if(pCurrentVesselCell1->cCover==pCurrentVesselCell1->coverlimit)
		//{
		//*SproutTips(I,J)=0;
		//}
		
		}//justgrow
		
		
		
		}
		
	
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }
	 
	 else if(RandNumber >=*Inf3(i,j) && RandNumber < *Sup3(i,j))
	 {
		I = i;
		J = j + 1;
	
		if( (*BloodVesselIndicator(I, J))> 0)
		{
		 
		 if(*Stability(I,J)>0)
		{
           *Stability(I,J) = *Stability(I,J) - AngioDeltaT * Stable_down;
		   if(*Stability(I,J) < 0)
          {
			*Stability(I,J) = 0;
		  }
		  /****/
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
			SproutNumber -= 1;//comment this we have anasmosis
			
		  }
		  
		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;
		 *SproutTips(i, j) = 0;	
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 Connect(pCurrentVesselCell1,pTempVesselCell);
		/***/
		   
		  
		
		}
		else
		{
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
				SproutNumber -= 1;
				  

		  }
		 
			  
		
		// SproutNumber -= 1;
		// *DirectionY(I, J) = 1;
		 //*SproutTips(I, J) = 1;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	 
		 *SproutTips(i, j) = 0;
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 Connect(pCurrentVesselCell1,pTempVesselCell);
		}
		 
		}
		else
		{
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->i_origin = i_origin;
		pCurrentVesselPoint2->j_origin = j_origin;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob3 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		//*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionY(I, J) = 1;
		
		
		//cout<<"here"<<endl;
		pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		//cout<<pCurrentVesselCell1->cIndex<<endl;
		
		double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
		double sezi;
		sezi= float( rand() ) / float( RAND_MAX);
		if(sezi<Prob)
		{
		
		TheVessel->CellListLength++;
		VesselCell* pNewVesselCell = new VesselCell;
	    pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
	    pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
		pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
		pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
		pCurrentVesselCell2->cCover = 1;
		pCurrentVesselCell2->i[0] = I;
		pCurrentVesselCell2->j[0] = J;
		pCurrentVesselCell2->pNextVesselCell = NULL;
		//cout<<pCurrentVesselCell2->cIndex<<endl;
		//pCurrentVesselCell2->cConnection = 1;
	    //pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
		
		Connect(pCurrentVesselCell2,pCurrentVesselCell1);
		*CellColor(I,J) = 100;

		
		
		}//mitosis and grow
		else
		{
		
		pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
		pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
		pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
		
		*CellColor(I,J) = 100;
		//if(pCurrentVesselCell1->cCover==pCurrentVesselCell1->coverlimit)
		//{
		//*SproutTips(I,J)=0;
		//}
		
		}//justgrow
		
		}
	
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }
	 
	 else if(RandNumber >= *Inf4(i,j) && RandNumber < *Sup4(i,j))
	 {
		I = i;
		J = j - 1;
	
				
		if( (*BloodVesselIndicator(I, J)) > 0)
		{
		
		if(*Stability(I,J)>0)
		{
           *Stability(I,J) = *Stability(I,J) - AngioDeltaT * Stable_down;
		   if(*Stability(I,J) < 0)
          {
			*Stability(I,J) = 0;
		  }
		   /****/
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
			SproutNumber -= 1;//comment this we have anasmosis
			
		  }
		  
		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;
		 *SproutTips(i, j) = 0;	
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 Connect(pCurrentVesselCell1,pTempVesselCell);
		/***/
		  
		  
		
		}
		else
		{
		 
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
			 SproutNumber -= 1;
		  }
		 		  
		 
		// *DirectionY(I, J) = -1;
		// *SproutTips(I, J) = 1;
		 //SproutNumber -= 1;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	
		 *SproutTips(i, j) = 0;
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 Connect(pCurrentVesselCell1,pTempVesselCell);

		}
		 
		}
		else
		{		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->i_origin = i_origin;
		pCurrentVesselPoint2->j_origin = j_origin;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob4 make Vessel Length"<<VesselListLength<<endl;

		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		//*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionY(I, J) = -1;
		
		pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		
		double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
		double sezi;
		sezi= float( rand() ) / float( RAND_MAX);
		if(sezi<Prob)
		{
		
		TheVessel->CellListLength++;
		VesselCell* pNewVesselCell = new VesselCell;
	    pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
	    pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
		pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
		pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
		pCurrentVesselCell2->cCover = 1;
		pCurrentVesselCell2->i[0] = I;
		pCurrentVesselCell2->j[0] = J;
		pCurrentVesselCell2->pNextVesselCell = NULL;
		//pCurrentVesselCell2->cConnection = 1;
	    //pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
		Connect(pCurrentVesselCell2,pCurrentVesselCell1);
		*CellColor(I,J) = 100;
		
		
		}//mitosis and grow
		else
		{
		
		pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
		pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
		pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
		
		*CellColor(I,J) = 100;
		//if(pCurrentVesselCell1->cCover==pCurrentVesselCell1->coverlimit)
		//{
		//*SproutTips(I,J)=0;
		//}
		
		}//justgrow
		
		
		}
	
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }

	//BOUNDARY SPROUTS We can ask it to be periodical?
    }//probtotal>0
	else
	{
	//*SproutTips(i,j) = 0;
	}//probtotal=0
	
	}//this is for the if sprout
		
	
	pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	 //cout<<"aftermove";
    }//this is for the loop
   pLastVesselPoint = pCurrentVesselPoint2;
   TheVessel->pLastVesselCell = pCurrentVesselCell2;
   //QuickPlot();// COMBINE
   cout <<"GrowSprout once"<<endl; 
   if(growstill==3)
   {
   
   pCurrentVesselPoint1 = pFirstVesselPoint;
   
   
   
   while(pCurrentVesselPoint1)
	{
		 pCurrentVesselPoint1->i_origin = pCurrentVesselPoint1->i;
		 pCurrentVesselPoint1->j_origin = pCurrentVesselPoint1->j;
		
	
			  
	  pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	}

   
   
   
   setorigin = 1;
   
   
   
   
   cout <<"origin reset"<<endl; 
   
   }//setorigin 
   
   
   
   }//still loop
   cout <<"function GrowSprout finish"<<endl;
 }
	
 
void VesselStructure:: Proliferate()
{
	//cout <<"function UpdateConductivity running"<<endl;
	extern Matrix ConductX, ConductY;
	extern Matrix BloodVesselRadiusXdir;
	extern Matrix BloodVesselRadiusYdir;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix VisX, VisY;
	extern int Xnodes,Ynodes;
	extern Matrix VesselCells;
	extern Matrix SproutTips;
	extern Matrix VEGFR;
	extern Matrix Notch;
	extern Matrix Delta4;
	double pi = 3.141592654;
	
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
	double RandNumber1 = 0.0;
	double RandNumber2 = 0.0;
	double k_v_proliferate =  RetrieveDoubleParameter( ParameterFile , "k_v_proliferate");
	double k_v_transit =  RetrieveDoubleParameter( ParameterFile , "k_v_transit");
	double min_vr =  RetrieveDoubleParameter( ParameterFile , "min_vr");
	double k_n_d_transit =  RetrieveDoubleParameter( ParameterFile , "k_n_d_transit");
	double min_n =  RetrieveDoubleParameter( ParameterFile , "min_n");
	double k_n_d_proliferate =  RetrieveDoubleParameter( ParameterFile , "k_n_d_proliferate");
	double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
	double r = 0;
	double N = 0;

	Matrix Prob_P = zeros(Xnodes, Ynodes);
	Matrix Prob_D = zeros(Xnodes, Ynodes);
	
	while(pCurrentVesselPoint)
	{
		int i = pCurrentVesselPoint->i;
		int j = pCurrentVesselPoint->j;
		if((pCurrentVesselPoint->MyAge) < VesselAgeInf)
		{
		N = 1;//*VesselCells(i,j);
		for(int k=0; k< N; k++)
		{
		
	
		*Prob_P(i,j) = Prob_Proliferate(i,j, k_v_proliferate, k_n_d_proliferate);
		
		RandNumber1 = float( rand() ) / float( RAND_MAX);
		if(RandNumber1<*Prob_P(i,j))
		{
			*VesselCells(i,j) += 1;
			CellNumber +=1;
		}//proliferate
					
					
		*Prob_D(i,j) = Prob_Transit(i, j, k_v_transit, k_n_d_transit);
			RandNumber2 = float( rand() ) / float( RAND_MAX);
			if(RandNumber2<*Prob_D(i,j))
			{
			*SproutTips(i,j) += 1;
			SproutNumber +=1;
			*VesselCells(i,j) -= 1;
			CellNumber -=1;
			}//tipcell
			
			
		
		}//proliferate and transit candidate
		
		}//if young
		
		
		
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
			
	}
	 /***********Normalize************/

  
 

	//cout <<"function UpdateConductivity finished"<<endl;
}
 
double Prob_Proliferate(int i, int j, double k_v_proliferate, double k_n_d_proliferate)
{
extern Matrix TAF;
extern Matrix VEGFR;
extern Matrix Notch;
extern Matrix Delta4;
double output = 0;

output = *TAF(i,j)/(1+k_n_d_proliferate* *Notch(i,j));

//output =( k_v_proliferate*(*TAF(i,j))/(1+*TAF(i,j)/ *VEGFR(i,j))-k_n_d_proliferate * *Delta4(i,j) * *Notch(i,j)/(*Delta4(i,j)+*Notch(i,j)))/(k_v_proliferate* *VEGFR(i,j));
if(output<0)
{
output = 0;
}
return output;
}

double Prob_Differentiate(int i, int j, double k_n_d_transit, double min_n)
{
extern Matrix TAF;
extern Matrix Notch;
extern Matrix Delta4;
extern Matrix SproutTips;
double output = 0;
output = 0;//1-1/(1+k_n_d_transit*min_n);//mightchange
if(output<0)
{
output = 0;
}
return output;

}

double Prob_Transit(int i, int j, double k_v_transit, double k_n_d_transit)
{
extern Matrix TAF;
extern Matrix Notch;
extern Matrix Delta4;
extern Matrix SproutTips;
extern Matrix VEGFR;
double output = 0;
//output = (k_v_transit*(*TAF(i,j))/(1+*TAF(i,j)/ *VEGFR(i,j))-k_n_d_transit* *Delta4(i,j) * *Notch(i,j)/(*Delta4(i,j)+*Notch(i,j)))/(k_v_transit* *VEGFR(i,j));
output = *TAF(i,j)/(1+k_n_d_transit * *Delta4(i,j) * *Notch(i,j));

if(output<0)
{
output = 0;
}
return output;

}

void VesselStructure:: CellDiffuse()
{
	//cout <<"function UpdateConductivity running"<<endl;
	extern Matrix ConductX, ConductY;
	extern Matrix BloodVesselRadiusXdir;
	extern Matrix BloodVesselRadiusYdir;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix VisX, VisY;
	extern int Xnodes,Ynodes;
	extern Matrix VesselCells;
	extern Matrix SproutTips;
	extern Matrix VEGFR;
	double pi = 3.141592654;
	
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
	double RandNumber1 = 0.0;
	double RandNumber2 = 0.0;
	
	bool DONE = 0;
	

	
	while(pCurrentVesselPoint)
	{
		int i = pCurrentVesselPoint->i;
		int j = pCurrentVesselPoint->j;
		DONE = 0;
		
				
		
		/**		
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5&&!DONE)
		{
		
		if(*VesselCells(i+1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i+1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		else if(*VesselCells(i-1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i-1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		
		}//both connected 
		
		if(*BloodVesselIndicatorX(i,j)>0.5&&!DONE)
		{
		if(*VesselCells(i+1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i+1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		}// right
		
		if(*BloodVesselIndicatorX(i,j)<-0.5&&!DONE)
		{
		
		if(*VesselCells(i-1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i-1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		
		}//left
		
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5&&!DONE)
		{
		if(*VesselCells(i,j+1)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i,j+1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		else if(*VesselCells(i,j-1)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i,j-1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}

		}//both connected Y
		
		if(*BloodVesselIndicatorY(i,j)>0.5&&!DONE)
		{
		if(*VesselCells(i,j+1)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i,j+1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		}// TOP
		
		if(*BloodVesselIndicatorY(i,j)<-0.5&&!DONE)
		{
		if(*VesselCells(i,j-1) -*VesselCells(i,j)>= 2)
		{
		*VesselCells(i,j-1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		}//BOTTOM
		
		
		*/
		if(*SproutTips(i,j)>0)
		{
		
			if(min_abs(*BloodVesselIndicatorX(i,j))>1.5&&!DONE)
		{
		
		if(*VesselCells(i+1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i+1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		else if(*VesselCells(i-1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i-1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		
		}//both connected 
		
		if(*BloodVesselIndicatorX(i,j)>0.5&&!DONE)
		{
		if(*VesselCells(i+1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i+1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		}// right
		
		if(*BloodVesselIndicatorX(i,j)<-0.5&&!DONE)
		{
		
		if(*VesselCells(i-1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i-1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		
		}//left
		
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5&&!DONE)
		{
		if(*VesselCells(i,j+1)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i,j+1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		else if(*VesselCells(i,j-1)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i,j-1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}

		}//both connected Y
		
		if(*BloodVesselIndicatorY(i,j)>0.5&&!DONE)
		{
		if(*VesselCells(i,j+1)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i,j+1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		}// TOP
		
		if(*BloodVesselIndicatorY(i,j)<-0.5&&!DONE)
		{
		if(*VesselCells(i,j-1) -*VesselCells(i,j)>= 2)
		{
		*VesselCells(i,j-1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		}//BOTTOM
		
		}//if a sprout
		
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
			
	}
	 /***********Normalize************/

  
 

	//cout <<"function UpdateConductivity finished"<<endl;
}

void VesselStructure::UpdateVEGFR()
{
cout <<"function UpdateVEGFR running"<<endl;
		extern char* ParameterFile;
	//-----------------------------------------------------------------------------------
 extern int Xnodes, Ynodes; 
 extern Matrix WssX, WssY;
 extern Matrix FlowX, FlowY;
 extern Matrix SproutTips;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselRadius;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY, BloodVesselIndicator;
 extern Matrix BloodNodePressure;
 extern Matrix VesselAge;
 extern Matrix TAF;
 extern Matrix VesselCells;
 extern Matrix VEGFR;
 extern Matrix SproutTips;
 
double min_vr = RetrieveDoubleParameter( ParameterFile , "min_vr");
  double min_C = RetrieveDoubleParameter( ParameterFile , "min_C");
 //extern Matrix DirectionX, DirectionY;
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 
    int i, j;

 VEGFR = zeros(Xnodes,Ynodes);
 


 
 while( pCurrentVesselPoint)
 {
 
	i = pCurrentVesselPoint-> i;
	j = pCurrentVesselPoint-> j;
	*VEGFR(i,j) = min_vr;

 
pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 	
 }
  cout <<"function UpdateVEGFR finished"<<endl;

}

void VesselStructure::UpdateNotch()
{
cout <<"function UpdateNotch running"<<endl;
		extern char* ParameterFile;
	//-----------------------------------------------------------------------------------
 extern int Xnodes, Ynodes; 
 extern Matrix WssX, WssY;
 extern Matrix FlowX, FlowY;
 extern Matrix SproutTips;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselRadius;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY, BloodVesselIndicator;
 extern Matrix BloodNodePressure;
 extern Matrix VesselAge;
 extern Matrix TAF;
 extern Matrix VesselCells;
 extern Matrix VEGFR;
 extern Matrix SproutTips;
 extern Matrix Notch;
 

  double min_n = RetrieveDoubleParameter( ParameterFile , "min_n");
 //extern Matrix DirectionX, DirectionY;
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 
    int i, j;

 Notch = zeros(Xnodes,Ynodes);
 


 
 while( pCurrentVesselPoint)
 {
 
	i = pCurrentVesselPoint-> i;
	j = pCurrentVesselPoint-> j;
	*Notch(i,j) = min_n;

 
pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 	
 }
  cout <<"function UpdateNotch finished"<<endl;

}


void VesselStructure::UpdateDelta4()
{
cout <<"function UpdateDelta4 running"<<endl;
		extern char* ParameterFile;
	//-----------------------------------------------------------------------------------
 extern int Xnodes, Ynodes; 
 int i, j, i_minus, j_minus, i_plus, j_plus;
 extern Matrix WssX, WssY;
 extern Matrix FlowX, FlowY;
 extern Matrix SproutTips;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselRadius;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY, BloodVesselIndicator;
 extern Matrix BloodNodePressure;
 extern Matrix VesselAge;
 extern Matrix TAF;
 extern Matrix VesselCells;
 extern Matrix VEGFR;
 extern Matrix SproutTips;
 extern Matrix Delta4;

  double k_v_dll = RetrieveDoubleParameter( ParameterFile , "k_v_dll");
 //extern Matrix DirectionX, DirectionY;
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;


 Delta4 = zeros(Xnodes,Ynodes);
 


 
 while( pCurrentVesselPoint)
 {
 
	i = pCurrentVesselPoint-> i;
	j = pCurrentVesselPoint-> j;
	
	
	double N = *SproutTips(i,j)+*VesselCells(i,j)-1;
	if(N>0)
	{
	*Delta4(i,j) = N*k_v_dll*(*TAF(i,j));//N*(k_v_dll*(*TAF(i,j))/(1+*TAF(i,j)/ *VEGFR(i,j)));
	}
	
if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}
		
		

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{       
			
			*Delta4(i,j) += k_v_dll*(*TAF(i_minus,j));//*(*TAF(i_minus,j))/(1+*TAF(i_minus,j)/ *VEGFR(i_minus,j));
						
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
	
			*Delta4(i,j) += k_v_dll*(*TAF(i_plus,j));//*(*TAF(i_plus,j))/(1+*TAF(i_plus,j)/ *VEGFR(i_plus,j));										
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
				
	        *Delta4(i,j) += k_v_dll*(*TAF(i,j_minus));//*(*TAF(i,j_minus))/(1+*TAF(i,j_minus)/ *VEGFR(i,j_minus));	
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
		    *Delta4(i,j) += k_v_dll*(*TAF(i,j_plus));//*(*TAF(i,j_plus))/(1+*TAF(i,j_plus)/ *VEGFR(i,j_plus));						
	}
	

 // cout<<"BVR"<<*BloodVesselRadius(i,j);



 
pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 	
 }
  cout <<"function UpdateDelta4 finished"<<endl;

}





 void VesselStructure::UpdatePermeability()
{
cout <<"function Permeability running"<<endl;
		extern char* ParameterFile;
	//-----------------------------------------------------------------------------------
 extern int Xnodes, Ynodes; 
 int i, j, i_minus, j_minus, i_plus, j_plus;
 extern Matrix WssX, WssY;
 extern Matrix FlowX, FlowY;
 extern Matrix SproutTips;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselRadius;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY, BloodVesselIndicator;
 extern Matrix BloodNodePressure;
 extern Matrix VesselAge;
 extern Matrix TAF;
 extern Matrix VesselCells;
 extern Matrix VEGFR;
 extern Matrix SproutTips;
 extern Matrix Delta4;
 extern Matrix Permeability;
 extern Matrix Conduct;
 extern Matrix InterPressure;
double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
double Perm = RetrieveDoubleParameter( ParameterFile , "Perm");
double Perm_o = RetrieveDoubleParameter( ParameterFile , "Perm_o");
extern Matrix Phi;
  double k_v_dll = RetrieveDoubleParameter( ParameterFile , "k_v_dll");
  
   double pi=3.141592654;



 //extern Matrix DirectionX, DirectionY;
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;

Conduct =zeros(Xnodes, Ynodes);
	//Conduct =ones(Xnodes, Ynodes);


 for(i=0; i<Xnodes; i++)
 {
	for(j=0; j<Ynodes; j++)
	{
	//cout<<*BloodVesselIndicatorX(ii,jj)<<endl;
	*Permeability(i,j) = 0;
	//*Permeability(ii,jj)=0.000001;
		//*InterPressure(i,j)= 2500.0;
	
		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{    
		*Conduct(i,j) += pi * *BloodVesselRadiusXdir(i, j) * 1.0e4;// 0.5overall effect ofS/V
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
		*Conduct(i,j) += pi * *BloodVesselRadiusXdir(i_plus, j) * 1.0e4;				
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
		*Conduct(i,j) += pi * *BloodVesselRadiusYdir(i, j) * 1.0e4;	
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
		
		*Conduct(i,j) += pi * *BloodVesselRadiusYdir(i, j_plus) * 1.0e4;	
	}
//*Conduct(i,j) = 1;//overall effect ofS/V ~ 0.02/mum r should be convert to mum
	
	
	}
 }
 



 

 
 
 
 while( pCurrentVesselPoint)
 {
 
	 i = pCurrentVesselPoint-> i;
	 j = pCurrentVesselPoint-> j;
	
	//if(*VesselAge(i,j)>=VesselAgeInf)
	//{
	//*Permeability(i,j)=0;//0.000001* *TAF(i,j);	
    //}
	//else
	if(*Phi(i,j)<0)
	{
		*Permeability(i,j) = Perm;
	}
	else
	{
		*Permeability(i,j) = Perm_o;
	}

	


 
pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 	
 }
  cout <<"function Permeability finished"<<endl;

}


 void VesselStructure::WSSTAFBranchByProbInterval(Matrix& Shuffle)// We can develop others by physiological observation too,WAIT UNTIL 3 D,,,,THERE ARE SO MANY PROBLEM!!OR...I INVENT SOME CRITERIA
{
	cout <<"WSSTAFBRANCHfunctionrunning"<<endl;

extern Matrix WssX, WssY;
double SproutAgeThreshold = RetrieveDoubleParameter( ParameterFile , "SproutAgeThreshold");
double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
double VesselAgeSup = RetrieveDoubleParameter( ParameterFile , "VesselAgeSup");
double k_n_d_transit = RetrieveDoubleParameter( ParameterFile , "k_n_d_transit");


extern int Xnodes, Ynodes;
extern Matrix TAF;
extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern double t;

//if( MaxWss < 1e-5 )
	{ MaxWss = 1.0; }
double RandNumber;
double ProbBranch;
int RandBranchDirection;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint;
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint;
 
 ofstream myfile13;
  myfile13.open ("debug.txt");
 
  

 

while ( pCurrentVesselPoint1 )
{
 int i = pCurrentVesselPoint1-> i;
 int j = pCurrentVesselPoint1-> j;
 /*  cout << "else if: " << (i>1) << ", " << i << endl;
  cout << "else if: " << (i<Xnodes-2) << endl;
  cout << "else if: " << (*SproutTips(i, j) < 1.0e-5) << ", " << *SproutTips(i,j) << endl;
  cout << "else if: " << (*VesselAge(i, j) > VesselAgeInf) << ", " << *VesselAge(i,j) << ", " << VesselAgeInf << endl;
  cout << "else if: " << (*VesselAge(i, j) < VesselAgeSup) << ", " << VesselAgeSup << endl;
  cout << "if: " << (*SproutAge(i, j) > SproutAgeThreshold) << ", " << *SproutAge(i,j) << ", " << SproutAgeThreshold << endl;
 */
// if(t>=0.05)
// {
//cout<<" age  "<<*VesselAge(i,j);
// getchar();
// }
   if( (i>1)&&(i<Xnodes-2)&&*SproutTips(i, j) < 1e-5&&*VesselAge(i, j)>0.6&&*VesselAge(i,j)<0.6)
  {
		//cout << "I am wrong" << endl;
       
			ProbBranch = *TAF(i,j)/(k_n_d_transit* *TAF(i,j)+MaxTAF);
		
		
		 RandNumber = float(rand())/float(RAND_MAX);
         RandBranchDirection = 1+int(2*float(rand())/float(RAND_MAX));
		if( RandNumber < ProbBranch )
		{
		  int Grow = 0;
		if( *BloodVesselIndicatorX(i, j) < 0)
		   {
				int I = i + 1;
				int J = j;
		  if( (*BloodVesselIndicator(I, J)) < 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
			if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

		    
			    }
			
		
		if( RandBranchDirection == 1)
		{
		  I = i;
		  J = j + 1;
		   if(*BloodVesselIndicator(I, J)< 10e-16)//&&*Shuffle(I, J)<0.5
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionY(I, J) = 1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));		
		}  
		}
		else if ( RandBranchDirection == 2)
		{
		  I = i;
		  J = j - 1;
		if( *BloodVesselIndicator(I,J)< 10e-16)//&&*Shuffle(I, J)<0.5
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = -1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			
			}
		
		  }
		
		}
		else if( *BloodVesselIndicatorX(i, j) > 0)
		{
		int I = i - 1;
		int J = j;
		
		  if( *BloodVesselIndicator(I, J) < 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		    //*DirectionX(I, J) = -1;
				if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		//}  
		if( RandBranchDirection == 1)
		{
		  I = i;
		  J = j - 1;
		if( *BloodVesselIndicator(I, J) < 10e-16)//&&*Shuffle(I, J)<0.5
		{   Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		 //   *SproutAge(i, j) = 0;
		  //  *DirectionY(I, J) = -1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

		} 
		}
		else if ( RandBranchDirection == 2)
		{
		  I = i;
		  J = j + 1;
		if( *BloodVesselIndicator(I, J)< 10e-16)//
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = 1;

		 		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));		} 
		
		}
		}
		
		else if( *BloodVesselIndicatorY(i, j) < 0)
		   {
				int I = i ;
				int J = j + 1;
		   if( *BloodVesselIndicator(I, J) < 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = 1;
		  		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));			    }
	
		
		if( RandBranchDirection == 1)
		{
		  I = i + 1;
		  J = j ;
          if( *BloodVesselIndicator(I, J) < 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
		  if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		}
 
		else if ( RandBranchDirection == 2)
		{
		  I = i - 1;
		  J = j ;
		 if( *BloodVesselIndicator(I, J) < 10e-16)//
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionX(I, J) = -1;
		  		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));			    }
		
		    }
		}
		
		else if( *BloodVesselIndicatorY(i, j) > 0)
		{
		int I = i ;
		int J = j - 1;
		  if( *BloodVesselIndicator(I, J) < 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		    //*DirectionY(I, J) = -1;
	if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		

		//}  
		if( RandBranchDirection == 1)
		{
		  I = i - 1;
		  J = j;
	    if(*BloodVesselIndicator(I, J)< 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		  //  *DirectionX(I, J) = -1;
		   		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J)); 
			    }

		}  
		else if ( RandBranchDirection == 2)
		{
		  I = i + 1;
		  J = j;
		 if( *BloodVesselIndicator(I, J)< 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
		 if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

			    }
		}
  
  }//DIRECTIONY END
  
  if(Grow > 1)
  {
  SproutNumber++;
  }
  
  
  }//IF PROB END
  
  
  }//SPROUT END
 
  //else if( (i>1)&&(i<Xnodes-2)&&(j>1)&&(j<200)&&*SproutTips(i, j) < 1e-5 && *VesselAge(i, j) > VesselAgeInf && *VesselAge(i, j) < VesselAgeSup)

  else if( (i>1)&&(i<Xnodes-2)&&*SproutTips(i, j) < 1e-5 && *VesselAge(i, j) >=VesselAgeInf && *VesselAge(i, j) < VesselAgeSup)
  {
	if( *WssX(i, j) <= 0.2 * MaxWss )
	{
	ProbBranch = 0.0;
	}
	
	else if ( *WssX(i, j) > 0.2 * MaxWss && *WssX(i, j) <= 0.4 * MaxWss )
	{
	    if(*TAF(i, j) <= 0.4 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.4 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.02;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.03;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.1;
		}
	}
	
	else if ( *WssX(i, j) > 0.4 * MaxWss && *WssX(i, j) <= 0.6 * MaxWss )
    {
		if(*TAF(i, j) <= 0.4 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.4 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.2;
		}
	}
	
	else if ( *WssX(i, j) > 0.6 * MaxWss && *WssX(i, j) <= 0.8 * MaxWss )
    {
		if(*TAF(i, j) <= 0.4 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.4 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.09;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.3;
		}
	}
  
	else if ( *WssX(i, j) > 0.8 * MaxWss )
	{
		if(*TAF(i, j) <= 0.4 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.4 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.16;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.4;
		}
	} 
	
	if(*WssX(i,j)>=1)
	{
	ProbBranch = *TAF(i,j)/(k_n_d_transit* *TAF(i,j)+MaxTAF);
	}
	else
	{
	ProbBranch =*WssX(i,j)* *TAF(i,j)/(k_n_d_transit* *TAF(i,j)+MaxTAF);

	}
	// X direction
	     RandNumber = float(rand())/float(RAND_MAX);
        RandBranchDirection = 1+int(2*float(rand())/float(RAND_MAX));
		
		//if(j==Ynodes-1)
		//{
		//cout << "********* 2 **********" << endl;
		//getchar();
		//myfile13 <<MaxWss<<" MaxWss "<<*WssX(i,j)<<" Wss "<<MaxTAF<<" MaxTAF "<<*TAF(i,j)<<" TAF "<<ProbBranch<<" ProbBranch "<<RandNumber<<" RandNumber "<<RandBranchDirection<<" RandBranchDirection "<<endl;
		//}
	
	if(RandNumber < ProbBranch && min_abs(*BloodVesselIndicatorY(i, j))<1.5 )
	{
	 int Grow = 0;
	if(RandBranchDirection == 1&&j<(Ynodes-1))
	   {
	      int I = i;
		  int J = j + 1;
		   if(min_abs(*BloodVesselIndicator(I, J))<10e-5)//&&*Shuffle(I, J)<0.5
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionY(I, J) = 1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		      {
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		      }
		else
			{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
			    {*BloodVesselIndicatorY(i,j)=2;}
		else
			    {*BloodVesselIndicatorY(i,j)=1;}
			}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
				{
		         *BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
				}
		else
				{
		if(*BloodVesselIndicatorY(I,J)>0.5)
				{*BloodVesselIndicatorY(I,J)=2;}
		else
				{*BloodVesselIndicatorY(I,J)=-1;}
				}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));	
			}	
	
	
		}
	else if( RandBranchDirection == 2&&j>0 )
	{
	int I = i;
	int J = j - 1;
	
	if( *BloodVesselIndicator(I,J)< 10e-16)//&&*Shuffle(I, J)<0.5
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = -1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			
		}
		
	  }
	if(Grow > 0)
  {
  SproutNumber++;
  }

	
   }
	
	
	
		 
	
	// Y Direction
	
	
	if( *WssY(i, j) <= 0.1 * MaxWss )//0.2
	{
	ProbBranch = 0.0;
	}
	
	else if ( *WssY(i, j) > 0.1 * MaxWss && *WssY(i, j) <= 0.2 * MaxWss )//0.2 0.4
	{
	    if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.02;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.03;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.1;
		}
	}
	
	else if ( *WssY(i, j) > 0.2 * MaxWss && *WssY(i, j) <= 0.4 * MaxWss )//0.4 0.6
    {
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.2;
		}
	}
	
	else if ( *WssY(i, j) > 0.4 * MaxWss && *WssY(i, j) <= 0.6 * MaxWss )//0.6 0.8
    {
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.09;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.3;
		}
	}
  
	else if ( *WssY(i, j) > 0.6 * MaxWss )//0.8
	{
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.16;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.4;
		}
	} 
	
	
	if(*WssY(i,j)>=1)
	{
	ProbBranch = *TAF(i,j)/(k_n_d_transit* *TAF(i,j)+MaxTAF);
	}
	else
	{
	ProbBranch =*WssY(i,j)* *TAF(i,j)/(k_n_d_transit* *TAF(i,j)+MaxTAF);

	}
	     RandNumber = float(rand())/float(RAND_MAX);
         RandBranchDirection = 1+int(2*float(rand())/float(RAND_MAX));
		 
		  if(RandNumber < ProbBranch && min_abs(*BloodVesselIndicatorX(i, j))<1.5)
		 {
		 int Grow = 0;
		 if(RandBranchDirection == 1 )
		 {
		 int I = i + 1;
		 int J = j;
		
		if( min_abs(*BloodVesselIndicator(I, J))< 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
		  if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		}
		 else if( RandBranchDirection == 2)
		 {
		 int I = i - 1;
		 int J = j;
      if( min_abs(*BloodVesselIndicator(I, J)) < 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionX(I, J) = -1;
		  		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));			   
		 }
		
		}
	if(Grow > 0)
  {
  SproutNumber++;
  }
	}


  }


 
pCurrentVesselPoint1 = pCurrentVesselPoint1 -> pNextVesselPoint;

}
 myfile13.close();

pLastVesselPoint = pCurrentVesselPoint2;
cout <<"function WSSTAFBranchByProbInterval finished"<<endl;
//extern Matrix DirectionY;

}


void VesselStructure::CleanUp( Matrix& Shuffle)
{
cout <<"function Cleanup running"<<endl; 
extern bool VesselBroke;
extern int touchpoints;
extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern Matrix BloodNodePressure;
extern Matrix TAF;
extern Matrix ECM;
extern int Xnodes, Ynodes;
double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
//sweep the list to update rather than matrix?
//yes!
//should we have more list?
//we don't need to, because even if we have two main vessel,
//it's like a whole system with the general BC and IC.

//DON'T FORGET THE INDICATOR
AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
AngioDeltaY = 1.0 /Ynodes;
Matrix Prob0 = zeros(Xnodes, Ynodes);
Matrix Prob1 = zeros(Xnodes, Ynodes);
Matrix Prob2 = zeros(Xnodes, Ynodes);
Matrix Prob3 = zeros(Xnodes, Ynodes);
Matrix Prob4 = zeros(Xnodes, Ynodes);
Matrix Inf0 = zeros(Xnodes, Ynodes);
Matrix Inf1 = zeros(Xnodes, Ynodes);
Matrix Inf2 = zeros(Xnodes, Ynodes);
Matrix Inf3 = zeros(Xnodes, Ynodes);
Matrix Inf4 = zeros(Xnodes, Ynodes);
Matrix Sup0 = zeros(Xnodes, Ynodes);
Matrix Sup1 = zeros(Xnodes, Ynodes);
Matrix Sup2 = zeros(Xnodes, Ynodes);
Matrix Sup3 = zeros(Xnodes, Ynodes);
Matrix Sup4 = zeros(Xnodes, Ynodes);
int iminus,iplus,jminus,jplus;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list

int LoopLength = VesselListLength; 

cout<<"Growth Begin"<<endl;
for(int Loop = 0; Loop < LoopLength; Loop++)
{
   //cout<<"enter loop"<<endl;
	int i = pCurrentVesselPoint1->i;
	int j = pCurrentVesselPoint1->j;
	
 // cout<<i<<"    "<<j<<endl;
	//if(*Shuffle(i,j)>0&&pCurrentVesselPoint1->AmIArtery==0)
	if(*BloodNodePressure(i,j)<1.0)
	{
		//	cout<<"beginkill";
				
	//tune the indicator matrix
	    if (i == 0) {iminus=i;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=j;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = i;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = j;} else  {jplus = j+1;}
	
	  int I, J;
	  
	  I=iplus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=1;}
	  else if(*BloodVesselIndicatorX(I,J)<-0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	
	  I=iminus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=-1;}
	  else if(*BloodVesselIndicatorX(I,J)>0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
	
	 
	  I=i;
	  J=jplus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=1;}
	  else if(*BloodVesselIndicatorY(I,J)<-0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	  I=i;
	  J=jminus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=-1;}
	  else if(*BloodVesselIndicatorY(I,J)>0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
		
	  I=i;
	  J=j;
	  *BloodVesselIndicatorX(I,J)=0;
	  *BloodVesselIndicatorY(I,J)=0;	 	  
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			  
				
				  
					
	//tune the chain
					  
					  
	VesselPoint* pTempVesselPoint = pCurrentVesselPoint1;
	cout<<"i:"<<i<<",j:"<<j<<"  ";
		touchpoints++;
		cout<<"touchpoints"<<touchpoints<<"  ";

	
	
	if(pCurrentVesselPoint1->pNextVesselPoint)
	{
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=pCurrentVesselPoint1->pNextVesselPoint;
	(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	else
	{
	cout<<"the last point"<<endl;
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=NULL;
	//(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselPoint2= pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	 
	   pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
	   
	   
	  pTempVesselPoint->pPreviousVesselPoint	= NULL;
	  pTempVesselPoint->pNextVesselPoint = NULL;
	  pTempVesselPoint->i = 0;
	  pTempVesselPoint->j = 0;
	  pTempVesselPoint->AmIArtery = 0;
	  delete pTempVesselPoint;

	 VesselListLength=VesselListLength-1;
	 
	 
	 if(*SproutTips(i,j)>0)
	 {
		*SproutTips(i,j)=0;
		*SproutAge(i,j)=0;
		SproutNumber=SproutNumber-1;
	 
	 }
	 else
	 {
	 *VesselAge(i,j)=0;
	 }
	 
	
	
	
	
	
	
	
	}//this is for the kill of points
	
	
		 //cout<<"beginmove";
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	 //cout<<"aftermove";
    }//this is for the loop
   pLastVesselPoint = pCurrentVesselPoint2;
   QuickPlot();// COMBINE
   cout <<"function Cleanup finished"<<endl; 
 }

void UpdateStability(void)
{
extern Matrix Stability;
extern Matrix BloodVesselIndicator;
extern Matrix TAF;
extern Matrix Wss;
extern int Xnodes,Ynodes;
double Stable_down = RetrieveDoubleParameter( ParameterFile , "Stable_down");
double Stable_up = RetrieveDoubleParameter( ParameterFile , "Stable_up");

for (int j = 0; j < Ynodes; j++)
 {
for(int i = 0; i < Xnodes; i++)
  {
       //if(*TAF(i,j) > 0) 
       // {
        //   *Stability(i,j) = *Stability(i,j) - AngioDeltaT * Stable_down * *TAF(i,j);
	//	}
	//	else
		if(*BloodVesselIndicator(i,j) > 0)
		{
		   //*Stability(i,j) = *Stability(i,j) + AngioDeltaT * Stable_up;
		
      if(*Stability(i,j) < 0)
        {
			*Stability(i,j) = 0;
		}
	  else if(*Stability(i,j)>1)
	    {
			*Stability(i,j) = 1;
	    }

		}
		else
		{
		*Stability(i,j) = 0;
		}
	
	
	
	
     }
 }


}

void VesselStructure::Remove(CellVesselStructure* TheVessel)
{
	cout <<"function Remove running"<<endl; 
	extern bool VesselBroke;
	extern int touchpoints;
	extern Matrix SproutTips;
	extern Matrix SproutAge;
	extern Matrix VesselAge;
	extern Matrix Stability;
	//extern Matrix DirectionX;
	//extern Matrix DirectionY;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix BloodVesselIndicator;
	extern Matrix BloodNodePressure;
	extern Matrix TAF;
	extern Matrix ECM;
	extern int Xnodes, Ynodes;
	double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
	double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
	double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
	double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
	double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
	//sweep the list to update rather than matrix?
	//yes!
	//should we have more list?
	//we don't need to, because even if we have two main vessel,
	//it's like a whole system with the general BC and IC.
	
	//DON'T FORGET THE INDICATOR
	AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
	AngioDeltaY = 1.0 /Ynodes;
	Matrix Prob0 = zeros(Xnodes, Ynodes);
	Matrix Prob1 = zeros(Xnodes, Ynodes);
	Matrix Prob2 = zeros(Xnodes, Ynodes);
	Matrix Prob3 = zeros(Xnodes, Ynodes);
	Matrix Prob4 = zeros(Xnodes, Ynodes);
	Matrix Inf0 = zeros(Xnodes, Ynodes);
	Matrix Inf1 = zeros(Xnodes, Ynodes);
	Matrix Inf2 = zeros(Xnodes, Ynodes);
	Matrix Inf3 = zeros(Xnodes, Ynodes);
	Matrix Inf4 = zeros(Xnodes, Ynodes);
	Matrix Sup0 = zeros(Xnodes, Ynodes);
	Matrix Sup1 = zeros(Xnodes, Ynodes);
	Matrix Sup2 = zeros(Xnodes, Ynodes);
	Matrix Sup3 = zeros(Xnodes, Ynodes);
	Matrix Sup4 = zeros(Xnodes, Ynodes);
	int iminus,iplus,jminus,jplus;
	VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
	VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
	
    
	VesselCell* pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
	VesselCell* pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to find out the current cell respond to the point
	
	VesselCell* pTempVesselCell = TheVessel->pFirstVesselCell;//This is to delete
	extern Matrix Shuffle;
	//Shuffle = zeros(Xnodes, Ynodes);
	
	
	int LoopLength = VesselListLength; 
	
	
	for(int Loop = 0; Loop < LoopLength; Loop++)
	{
		//cout<<"give position"<<endl;
		int i = pCurrentVesselPoint1->i;
		int j = pCurrentVesselPoint1->j;
		 
		
		//if(*Shuffle(i,j)>0&&pCurrentVesselPoint1->AmIArtery==0)
		//if(*Shuffle(i,j) >0&&pCurrentVesselPoint1->AmIArtery==0&&pCurrentVesselPoint1->AmIVenous==0)
		if(*Shuffle(i,j) >0)
		{	 cout<<i<<"    "<<j<<endl;
			// cout<<*Shuffle(i,j);	
			cout<<"beginkill";
			pTempVesselCell = TheVessel->FindCell(i,j);
			//tune the indicator matrix
			if (i == 0) {iminus=i;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
			if (j == 0) {jminus=j;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
			if (i == Xnodes-1) {iplus = i;} else  {iplus = i+1;}
			if (j == Ynodes-1) {jplus = j;} else  {jplus = j+1;}
			
			int I, J;
			
			I=iplus;
			J=j;
			if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
			{*BloodVesselIndicatorX(I,J)=1;
				pCurrentVesselCell1 = TheVessel->FindCell(I,J);
				if(pCurrentVesselCell1 != pTempVesselCell)
				{				
					DisConnect(pTempVesselCell,pCurrentVesselCell1);
				}
				

			}
			else if(*BloodVesselIndicatorX(I,J)<-0.5)
			{*BloodVesselIndicatorX(I,J)=0;
				pCurrentVesselCell1 = TheVessel->FindCell(I,J);
				if(pCurrentVesselCell1 != pTempVesselCell)
				{				
					DisConnect(pTempVesselCell,pCurrentVesselCell1);
				}
			}
			*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			
			
			
			
			I=iminus;
			J=j;
			if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
			{*BloodVesselIndicatorX(I,J)=-1;
				pCurrentVesselCell1 = TheVessel->FindCell(I,J);
				if(pCurrentVesselCell1 != pTempVesselCell)
				{				
					DisConnect(pTempVesselCell,pCurrentVesselCell1);
				}
				
			}
			else if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=0;
			
				pCurrentVesselCell1 = TheVessel->FindCell(I,J);
				if(pCurrentVesselCell1 != pTempVesselCell)
				{				
					DisConnect(pTempVesselCell,pCurrentVesselCell1);
				}
				
			}
			*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			
			
			
			
			I=i;
			J=jplus;
			if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
			{*BloodVesselIndicatorY(I,J)=1;
				pCurrentVesselCell1 = TheVessel->FindCell(I,J);
				if(pCurrentVesselCell1 != pTempVesselCell)
				{				
					DisConnect(pTempVesselCell,pCurrentVesselCell1);
				}
				
			}
			else if(*BloodVesselIndicatorY(I,J)<-0.5)
			{*BloodVesselIndicatorY(I,J)=0;
				pCurrentVesselCell1 = TheVessel->FindCell(I,J);
				if(pCurrentVesselCell1 != pTempVesselCell)
				{				
					DisConnect(pTempVesselCell,pCurrentVesselCell1);
				}
				
			}
			*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			
			
			I=i;
			J=jminus;
			if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
			{*BloodVesselIndicatorY(I,J)=-1;
				pCurrentVesselCell1 = TheVessel->FindCell(I,J);
				if(pCurrentVesselCell1 != pTempVesselCell)
				{				
					DisConnect(pTempVesselCell,pCurrentVesselCell1);
				}
				
			}
			else if(*BloodVesselIndicatorY(I,J)>0.5)
			{*BloodVesselIndicatorY(I,J)=0;
				pCurrentVesselCell1 = TheVessel->FindCell(I,J);
				if(pCurrentVesselCell1 != pTempVesselCell)
				{				
					DisConnect(pTempVesselCell,pCurrentVesselCell1);
				}
				
			}
			*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			
			
			I=i;
			J=j;
			*BloodVesselIndicatorX(I,J)=0;
			*BloodVesselIndicatorY(I,J)=0;	 	  
			*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			
			
			
			
			
			
			
			
			
			
			
			
			
			//pTempVesselCell = TheVessel->FindCell(i,j);
			// cout<<pTempVesselCell->i[0]<<"  "<<pTempVesselCell->j[0]<<endl;
			if(pTempVesselCell->cCover > 1)
			{
				
				RemoveUnConnectedPoint(pTempVesselCell, i, j);	
				
			}
			else if(pTempVesselCell->cCover == 1)
			{
				pCurrentVesselCell1 = TheVessel->pFirstVesselCell; 
				pCurrentVesselCell1 = pCurrentVesselCell1->pNextVesselCell;
				
				while(pCurrentVesselCell1)
				{    
					if(Connected(pTempVesselCell,pCurrentVesselCell1))
					{
						cout<<"Before"<<endl;
						DisConnect(pTempVesselCell,pCurrentVesselCell1);
						cout<<"After"<<endl;
					}
					
					pCurrentVesselCell1 = pCurrentVesselCell1->pNextVesselCell;
				}
				if(pTempVesselCell->pNextVesselCell)
				{
					(pTempVesselCell->pPreviousVesselCell)->pNextVesselCell=pTempVesselCell->pNextVesselCell;
					(pTempVesselCell->pNextVesselCell)->pPreviousVesselCell=pTempVesselCell->pPreviousVesselCell;
				}
				else
				{
					cout<<"the last point"<<endl;
					(pTempVesselCell->pPreviousVesselCell)->pNextVesselCell=NULL;
					//(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
					pCurrentVesselCell2 = pTempVesselCell->pPreviousVesselCell;
					
				}
				
				
				
				
				pTempVesselCell->pPreviousVesselCell	= NULL;
				pTempVesselCell->pNextVesselCell = NULL;
				
				delete pTempVesselCell;
				
				TheVessel->pLastVesselCell = pCurrentVesselCell2;
				
			}
			
			
			
			//tune the chain
			
			
			VesselPoint* pTempVesselPoint = pCurrentVesselPoint1;
			cout<<"i:"<<i<<",j:"<<j<<"  ";
			touchpoints++;
			cout<<"touchpoints"<<touchpoints<<"  ";
			cout<<"begin loop"<<endl;
			
			
			
			if(pCurrentVesselPoint1->pNextVesselPoint&&pCurrentVesselPoint1->pPreviousVesselPoint)
			{cout<<"enter loop1"<<endl;
				(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=pCurrentVesselPoint1->pNextVesselPoint;
				(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
				pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
			}
			else if(pCurrentVesselPoint1->pNextVesselPoint)
			{
				cout<<"enter loop2"<<endl;
				(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=NULL;
				pFirstVesselPoint = pCurrentVesselPoint1->pNextVesselPoint;
				
				pCurrentVesselPoint1 = pFirstVesselPoint;
			}
			else
			{
				cout<<"enter loop3"<<endl;
				cout<<"the last point"<<endl;
				(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=NULL;
				//(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
				pCurrentVesselPoint2= pCurrentVesselPoint1->pPreviousVesselPoint;
				pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
			}
			
			
			
			
			
			
			
			
			
			
			
			pTempVesselPoint->pPreviousVesselPoint	= NULL;
			pTempVesselPoint->pNextVesselPoint = NULL;
			pTempVesselPoint->i = 0;
			pTempVesselPoint->j = 0;
			pTempVesselPoint->AmIArtery = 0;
			delete pTempVesselPoint;
			
			VesselListLength=VesselListLength-1;
			
			
			if(*SproutTips(i,j)>0)
			{
				*SproutTips(i,j)=0;
				*SproutAge(i,j)=0;
				SproutNumber=SproutNumber-1;
				
			}
			else
			{
				*VesselAge(i,j)=0;
			}
			
			
			cout<<"end loop"<<endl;
			
			
			if(pCurrentVesselPoint1 != pFirstVesselPoint)
			{
				pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
			}
			
			
			
			
		}//this is for the kill of points
		else
		{
			pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
		}
		
		//cout<<"beginmove";
		
		// cout<<"aftermove";
    }//this is for the loop
	pLastVesselPoint = pCurrentVesselPoint2;
	QuickPlot();// COMBINE
	Shuffle = zeros(Xnodes, Ynodes);
	cout <<"function Cleanup finished"<<endl; 
	
}


void VesselStructure::Retract(CellVesselStructure* TheVessel)
{
cout <<"function Retract running"<<endl; 
extern bool VesselBroke;
extern int touchpoints;
extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
extern Matrix Stability;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern Matrix BloodNodePressure;
extern Matrix TAF;
extern Matrix ECM;
extern int Xnodes, Ynodes;
double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
//sweep the list to update rather than matrix?
//yes!
//should we have more list?
//we don't need to, because even if we have two main vessel,
//it's like a whole system with the general BC and IC.

//DON'T FORGET THE INDICATOR
AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
AngioDeltaY = 1.0 /Ynodes;
Matrix Prob0 = zeros(Xnodes, Ynodes);
Matrix Prob1 = zeros(Xnodes, Ynodes);
Matrix Prob2 = zeros(Xnodes, Ynodes);
Matrix Prob3 = zeros(Xnodes, Ynodes);
Matrix Prob4 = zeros(Xnodes, Ynodes);
Matrix Inf0 = zeros(Xnodes, Ynodes);
Matrix Inf1 = zeros(Xnodes, Ynodes);
Matrix Inf2 = zeros(Xnodes, Ynodes);
Matrix Inf3 = zeros(Xnodes, Ynodes);
Matrix Inf4 = zeros(Xnodes, Ynodes);
Matrix Sup0 = zeros(Xnodes, Ynodes);
Matrix Sup1 = zeros(Xnodes, Ynodes);
Matrix Sup2 = zeros(Xnodes, Ynodes);
Matrix Sup3 = zeros(Xnodes, Ynodes);
Matrix Sup4 = zeros(Xnodes, Ynodes);
int iminus,iplus,jminus,jplus;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list

    
	VesselCell* pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
	VesselCell* pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to find out the current cell respond to the point

	VesselCell* pTempVesselCell = TheVessel->pFirstVesselCell;//This is to delete
extern Matrix Shuffle;
//Shuffle = zeros(Xnodes, Ynodes);
for(int i = 1; i<Xnodes-1; i++)
{
for(int j = 1; j<Ynodes-1; j++)
{

//if(*BloodVesselIndicator(i,j)==1&&*SproutTips(i,j)<1)
//if(*BloodVesselIndicator(i,j)==1&&*Stability(i,j)>0.5)
if(*BloodVesselIndicator(i,j)==1&&*TAF(i,j)>0.5)
//if(*BloodVesselIndicator(i,j)==1)	
{
*Shuffle(i,j) = 1;
}


}
}


int LoopLength = VesselListLength; 


for(int Loop = 0; Loop < LoopLength; Loop++)
{
   //cout<<"give position"<<endl;
	int i = pCurrentVesselPoint1->i;
	int j = pCurrentVesselPoint1->j;
 //  cout<<i<<"    "<<j<<endl;
// cout<<*Shuffle(i,j);	
//cout<<"beginkill";

	//if(*Shuffle(i,j)>0&&pCurrentVesselPoint1->AmIArtery==0)
	if(*Shuffle(i,j) >0&&pCurrentVesselPoint1->AmIArtery==0&&pCurrentVesselPoint1->AmIVenous==0)
	{	
							
	//tune the indicator matrix
	    if (i == 0) {iminus=i;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=j;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = i;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = j;} else  {jplus = j+1;}
	
	  int I, J;
	  
	  I=iplus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=1;}
	  else if(*BloodVesselIndicatorX(I,J)<-0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	
	  I=iminus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=-1;}
	  else if(*BloodVesselIndicatorX(I,J)>0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
	
	 
	  I=i;
	  J=jplus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=1;}
	  else if(*BloodVesselIndicatorY(I,J)<-0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	  I=i;
	  J=jminus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=-1;}
	  else if(*BloodVesselIndicatorY(I,J)>0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
		
	  I=i;
	  J=j;
	  *BloodVesselIndicatorX(I,J)=0;
	  *BloodVesselIndicatorY(I,J)=0;	 	  
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
		
	
	

	
	
	
	

	
	
	
	 pTempVesselCell = TheVessel->FindCell(i,j);
	// cout<<pTempVesselCell->i[0]<<"  "<<pTempVesselCell->j[0]<<endl;
	  if(pTempVesselCell->cCover > 1)
	  {
	  
		RemoveUnConnectedPoint(pTempVesselCell, i, j);	
		
	  }
	  else if(pTempVesselCell->cCover == 1)
	  {
	pCurrentVesselCell1 = TheVessel->pFirstVesselCell; 
	pCurrentVesselCell1 = pCurrentVesselCell1->pNextVesselCell;

  while(pCurrentVesselCell1)
  {    
  if(Connected(pTempVesselCell,pCurrentVesselCell1))
  {
  cout<<"Before"<<endl;
    DisConnect(pTempVesselCell,pCurrentVesselCell1);
	 cout<<"After"<<endl;
  }
  
  pCurrentVesselCell1 = pCurrentVesselCell1->pNextVesselCell;
  }
    if(pTempVesselCell->pNextVesselCell)
	{
	(pTempVesselCell->pPreviousVesselCell)->pNextVesselCell=pTempVesselCell->pNextVesselCell;
	(pTempVesselCell->pNextVesselCell)->pPreviousVesselCell=pTempVesselCell->pPreviousVesselCell;
	}
	else
	{
	cout<<"the last point"<<endl;
	(pTempVesselCell->pPreviousVesselCell)->pNextVesselCell=NULL;
	//(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselCell2 = pTempVesselCell->pPreviousVesselCell;
	 
	}
	
	   
	   
	   
	  pTempVesselCell->pPreviousVesselCell	= NULL;
	  pTempVesselCell->pNextVesselCell = NULL;
	 
	  delete pTempVesselCell;

	 TheVessel->pLastVesselCell = pCurrentVesselCell2;
	  
	  }
				
			  
					
	//tune the chain
					  
					  
	VesselPoint* pTempVesselPoint = pCurrentVesselPoint1;
	cout<<"i:"<<i<<",j:"<<j<<"  ";
		touchpoints++;
		cout<<"touchpoints"<<touchpoints<<"  ";
cout<<"begin loop"<<endl;
	

	
	if(pCurrentVesselPoint1->pNextVesselPoint&&pCurrentVesselPoint1->pPreviousVesselPoint)
	{cout<<"enter loop1"<<endl;
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=pCurrentVesselPoint1->pNextVesselPoint;
	(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	else if(pCurrentVesselPoint1->pNextVesselPoint)
	{
	cout<<"enter loop2"<<endl;
	(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=NULL;
	pFirstVesselPoint = pCurrentVesselPoint1->pNextVesselPoint;
	
	pCurrentVesselPoint1 = pFirstVesselPoint;
	}
	else
	{
	cout<<"enter loop3"<<endl;
	cout<<"the last point"<<endl;
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=NULL;
	//(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselPoint2= pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	
 
	   
	 	 
	   	   
	   
	   
	   
	   
	   
	   
	  pTempVesselPoint->pPreviousVesselPoint	= NULL;
	  pTempVesselPoint->pNextVesselPoint = NULL;
	  pTempVesselPoint->i = 0;
	  pTempVesselPoint->j = 0;
	  pTempVesselPoint->AmIArtery = 0;
	  delete pTempVesselPoint;

	 VesselListLength=VesselListLength-1;
	 
	 
	 if(*SproutTips(i,j)>0)
	 {
		*SproutTips(i,j)=0;
		*SproutAge(i,j)=0;
		SproutNumber=SproutNumber-1;
	 
	 }
	 else
	 {
	 *VesselAge(i,j)=0;
	 }
	 
	
	cout<<"end loop"<<endl;

	
	if(pCurrentVesselPoint1 != pFirstVesselPoint)
		 {
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
		 }

	
	
	
	}//this is for the kill of points
	else
	{
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	}
	
		 //cout<<"beginmove";
		 
	// cout<<"aftermove";
    }//this is for the loop
   pLastVesselPoint = pCurrentVesselPoint2;
   QuickPlot();// COMBINE
   Shuffle = zeros(Xnodes, Ynodes);
   cout <<"function Cleanup finished"<<endl; 

}



void VesselStructure::MakeList(void)
{
extern Matrix BloodVesselIndicator;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
Matrix Used;
Matrix Index;
extern int Xnodes, Ynodes;
extern double t;
Used = zeros(Xnodes,Ynodes);
Index = zeros(Xnodes, Ynodes);
int index=1;
int I,J;

 extern char SaveDirectory[1025];
 char temp_string[1024];
 
 /*********generate index and save position*******/
 
 //sprintf( Command, "./CompositeFrame %f frame%08i.bmp", time, frame );
 
 
 sprintf( temp_string, "%s/position_list_%f.txt", SaveDirectory,t);
 ofstream position_list( temp_string, ios::out );
			  
  
  VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 
 while(pCurrentVesselPoint)
 {
 I =  pCurrentVesselPoint->i;
 J =  pCurrentVesselPoint->j;
 *Index(I,J) = index;
 position_list  <<I<<"\t"
				<<J<<endl;
										
 pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;	
 index++;
								
 }
 
 
 position_list.close();	

 
 
 /*********generate branch list*****************/
 
 
 //save list now
  sprintf( temp_string , "%s/branch_list_%f.txt" , SaveDirectory,t );
 ofstream branch_list( temp_string, ios::out );
  pCurrentVesselPoint = pFirstVesselPoint;
 while(pCurrentVesselPoint)
 {
 I= pCurrentVesselPoint->i;
 J= pCurrentVesselPoint->j;

 if(*BloodVesselIndicator(I,J)>2)
 {
  *Used(I,J)=1;
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 I = pCurrentVesselPoint->i+1;
 J = pCurrentVesselPoint->j;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;
//again
I = pCurrentVesselPoint->i-1;
 J = pCurrentVesselPoint->j;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;



 
 
 
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1)
 {
  I = pCurrentVesselPoint->i+1;
 J = pCurrentVesselPoint->j;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;

 }
 else if(*BloodVesselIndicatorX(I,J)==-1)
 {
 I = pCurrentVesselPoint->i-1;
 J = pCurrentVesselPoint->j;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;

 }
 //Ydirection
  if(*BloodVesselIndicatorY(I,J)==2)
 {
 I = pCurrentVesselPoint->i;
 J = pCurrentVesselPoint->j+1;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;
//again
I = pCurrentVesselPoint->i;
J = pCurrentVesselPoint->j-1;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;



 
 
 
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1)
 {
  I = pCurrentVesselPoint->i;
 J = pCurrentVesselPoint->j+1;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;
 }
 else if(*BloodVesselIndicatorY(I,J)==-1)
 {
 I = pCurrentVesselPoint->i;
 J = pCurrentVesselPoint->j-1;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;

 }
 
 
 }
 
 pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;					
 }
 branch_list.close();
// cout <<"function SaveListInOrder finished"<<endl;



}
 
void VesselStructure::Remodel(CellVesselStructure* TheVessel)
{
cout <<"function Remodel running"<<endl; 
extern bool VesselBroke;
extern int touchpoints;
extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
extern Matrix Stability;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselPressureX;
extern Matrix BloodVesselPressureY;
extern Matrix WssX;
extern Matrix WssY;
extern Matrix BloodVesselIndicator;
extern Matrix BloodNodePressure;
extern Matrix TAF;
extern Matrix ECM;
 double Pin, Pout;
 double Ratio;
Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
Pout = RetrieveDoubleParameter( ParameterFile , "Pout");

extern int Xnodes, Ynodes;
double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
//sweep the list to update rather than matrix?
//yes!
//should we have more list?
//we don't need to, because even if we have two main vessel,
//it's like a whole system with the general BC and IC.

//DON'T FORGET THE INDICATOR
AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
AngioDeltaY = 1.0 /Ynodes;
int iminus,iplus,jminus,jplus;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list

    
	VesselCell* pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
	VesselCell* pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to find out the current cell respond to the point

	VesselCell* pTempVesselCell = TheVessel->pFirstVesselCell;//This is to delete


int LoopLength = VesselListLength; 


for(int Loop = 0; Loop < LoopLength; Loop++)
{
   //cout<<"give position"<<endl;
	int i = pCurrentVesselPoint1->i;
	int j = pCurrentVesselPoint1->j;
 //  cout<<i<<"    "<<j<<endl;
// cout<<*Shuffle(i,j);	
//cout<<"beginkill";

	//if(*Shuffle(i,j)>0&&pCurrentVesselPoint1->AmIArtery==0)
	if(*BloodVesselIndicator(i,j) > 2&&pCurrentVesselPoint1->AmIArtery==0&&pCurrentVesselPoint1->AmIVenous==0)
	{	
							
	//tune the indicator matrix
	    if (i == 0) {iminus=i;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=j;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = i;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = j;} else  {jplus = j+1;}
	
	  int I, J;
	  
	  if(min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5)
	  {
	  I=iplus;
	  J=j;
	  Ratio = *WssX(i,j)/MaxWss;
	  
	  double RandNumber = float(rand())/float(RAND_MAX); 
	// if()
	 {
	   if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=1;}
	  else if(*BloodVesselIndicatorX(I,J)<-0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		}
	  
	  }
	 	  
	   
		
	  
	 
	  
	
	  I=iminus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=-1;}
	  else if(*BloodVesselIndicatorX(I,J)>0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
	
	 
	  I=i;
	  J=jplus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=1;}
	  else if(*BloodVesselIndicatorY(I,J)<-0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	  I=i;
	  J=jminus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=-1;}
	  else if(*BloodVesselIndicatorY(I,J)>0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
		
	  I=i;
	  J=j;
	  *BloodVesselIndicatorX(I,J)=0;
	  *BloodVesselIndicatorY(I,J)=0;	 	  
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
		
	
	

	
	
	
	

	
	
	
	 pTempVesselCell = TheVessel->FindCell(i,j);
	 
	
	
	
	
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	}
	
		 //cout<<"beginmove";
		 
	// cout<<"aftermove";
    }//this is for the loop
   pLastVesselPoint = pCurrentVesselPoint2;
   QuickPlot();// COMBINE
   cout <<"function Cleanup finished"<<endl; 

}

void VesselStructure::InitialGrowSprout(CellVesselStructure* TheVessel)
{
	cout <<"function GrowSprout running"<<endl; 
	extern bool VesselBroke;
	extern int touchpoints;
	extern Matrix SproutTips;
	extern Matrix SproutAge;
	extern Matrix VesselAge;
	extern Matrix Stability;
	extern Matrix PreExist;
	//extern Matrix DirectionX;
	//extern Matrix DirectionY;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix BloodVesselIndicator;
	extern Matrix TAF;
	extern Matrix ECM;
	extern Matrix CellColor;
	extern int Xnodes, Ynodes;
	double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
	double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
	double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
	double String = RetrieveDoubleParameter( ParameterFile , "String");
	double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
	AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
	AngioDeltaY = 1.0 /Ynodes;
	Matrix Prob0 = zeros(Xnodes, Ynodes);
	Matrix Prob1 = zeros(Xnodes, Ynodes);
	Matrix Prob2 = zeros(Xnodes, Ynodes);
	Matrix Prob3 = zeros(Xnodes, Ynodes);
	Matrix Prob4 = zeros(Xnodes, Ynodes);
	Matrix Inf0 = zeros(Xnodes, Ynodes);
	Matrix Inf1 = zeros(Xnodes, Ynodes);
	Matrix Inf2 = zeros(Xnodes, Ynodes);
	Matrix Inf3 = zeros(Xnodes, Ynodes);
	Matrix Inf4 = zeros(Xnodes, Ynodes);
	Matrix Sup0 = zeros(Xnodes, Ynodes);
	Matrix Sup1 = zeros(Xnodes, Ynodes);
	Matrix Sup2 = zeros(Xnodes, Ynodes);
	Matrix Sup3 = zeros(Xnodes, Ynodes);
	Matrix Sup4 = zeros(Xnodes, Ynodes);
	int iminus,iplus,jminus,jplus;
	bool growstill=1;
	bool setorigin=0;
	int LoopLength = VesselListLength; 
	cout<<"Growth Begin"<<endl;
	VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
	VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
	VesselCell* pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
	VesselCell* pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
	VesselCell* pTempVesselCell = TheVessel->pFirstVesselCell;
	
	SproutTips = zeros(Xnodes,Ynodes);
	for(int i=12;i<=187;i=i+25)
	{
	for(int j=12;j<=187;j=j+25)
	{
	*SproutTips(i,j) = 1;
	}
	}
	
	while(growstill || !setorigin)
	{
		
		cout<<"Once Begin"<<endl;
		growstill = 0;
		
		
		Prob0 = zeros(Xnodes, Ynodes);
		Prob1 = zeros(Xnodes, Ynodes);
		Prob2 = zeros(Xnodes, Ynodes);
		Prob3 = zeros(Xnodes, Ynodes);
		Prob4 = zeros(Xnodes, Ynodes);
		Inf0 = zeros(Xnodes, Ynodes);
		Inf1 = zeros(Xnodes, Ynodes);
		Inf2 = zeros(Xnodes, Ynodes);
		Inf3 = zeros(Xnodes, Ynodes);
		Inf4 = zeros(Xnodes, Ynodes);
		Sup0 = zeros(Xnodes, Ynodes);
		Sup1 = zeros(Xnodes, Ynodes);
		Sup2 = zeros(Xnodes, Ynodes);
		Sup3 = zeros(Xnodes, Ynodes);
		Sup4 = zeros(Xnodes, Ynodes);
		
		
		pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
		pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
		pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
		pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
		pTempVesselCell = TheVessel->pFirstVesselCell;
		LoopLength = VesselListLength;
		VesselBroke=0;
		
		
		for(int Loop = 0; Loop < LoopLength; Loop++)
		{
			//cout<<"enter loop"<<endl;
			int i = pCurrentVesselPoint1->i;
			int j = pCurrentVesselPoint1->j;
			int i_origin = pCurrentVesselPoint1->i_origin;
			int j_origin = pCurrentVesselPoint1->j_origin;
			
			
			
			if((i==0||j==0||i==Xnodes-1||j==Ynodes-1)&&*SproutTips(i,j)==1)
			{
				*SproutTips(i,j)=0;
				//SproutNumber -=1;
			}
			
			
			if( *SproutTips(i,j) > 0)
			{
				
				
				//cout<<"enter sprouttip loop"<<endl;
				//cout<<"beginsprout";
				if (i == 0) {iminus=1;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
				if (j == 0) {jminus=1;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
				if (i == Xnodes-1) {iplus = Xnodes - 2;} else  {iplus = i+1;}
				if (j == Ynodes-1) {jplus = Ynodes - 2;} else  {jplus = j+1;}
				
				//Prob0 should be with stability 
				
				/*
				 *Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i, j) );
				 
				 *Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i, j) );
				 
				 *Prob3(i, j) = Chemo_Response*( *TAF(i, jplus) - *TAF(i, j) );	   
				 
				 *Prob4(i, j) = Chemo_Response*( *TAF(i, jminus) - *TAF(i, j) );
				 */
				*Prob0(i, j) = 0;
				*Prob1(i, j) = 0;
				*Prob2(i, j) = 0;
				*Prob3(i, j) = 0;
				*Prob4(i, j) = 0;
				
				*Prob1(i,j) = 1;
				
				//the sprout shouldn't grow reverse  
				if (*BloodVesselIndicatorX(i,j) < 0 )
				{
					*Prob2(i,j) = 0;
				}
				else if (*BloodVesselIndicatorX(i,j) > 0)
				{
					*Prob1(i,j) = 0;
				}
				else if (*BloodVesselIndicatorY(i,j) < 0 )
				{
					*Prob4(i,j) = 0;
				}
				else if (*BloodVesselIndicatorY(i,j) > 0 )
				{
					*Prob3(i,j) = 0;
				}
				//other exceptions 
				if (i == Xnodes-1) 
				{ *Prob1(i, j) = 0.0; }
				else if (i == 0)   
				{ *Prob2(i, j) = 0.0; }
				if (j == ( Ynodes-1))
				{ *Prob3(i, j) = 0.0; }
				if (j == 0)
				{ *Prob4(i, j) = 0.0; }
				
				
				// cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
				//rescale the probabilities
				double ProbTotal = *Prob0(i,j) + *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j) ;
				if ( ProbTotal> 0 )
				{
					growstill = 1;
					
					*Prob0(i,j) /= ProbTotal;
					*Prob1(i,j) /= ProbTotal;
					*Prob2(i,j) /= ProbTotal;
					*Prob3(i,j) /= ProbTotal;
					*Prob4(i,j) /= ProbTotal;
					
					//cout<<*Prob0(i,j)<<"  "<<*Prob1(i,j)<<"  " <<*Prob2(i,j)<<"  "<<*Prob3(i,j)<<"  " <<*Prob4(i,j)<<endl;
					
					//Get Random Intervals
					*Inf0(i,j) = 0;
					*Sup0(i,j) = *Inf0(i,j) + *Prob0(i,j);
					//*Sup0(i,j) = *Inf0(i,j) + *Prob1(i,j);
					*Inf1(i,j) = *Sup0(i,j);
					*Sup1(i,j) = *Inf1(i,j) + *Prob1(i,j);
					//*Sup1(i,j) = *Inf1(i,j) + *Prob0(i,j);
					*Inf2(i,j) = *Sup1(i,j);
					*Sup2(i,j) = *Inf2(i,j) + *Prob2(i,j);
					*Inf3(i,j) = *Sup2(i,j);
					*Sup3(i,j) = *Inf3(i,j) + *Prob3(i,j);
					//*Sup3(i,j) = *Inf3(i,j) + *Prob4(i,j);
					*Inf4(i,j) = *Sup3(i,j);   //not neccesary, delete them to speed up the program
					*Sup4(i,j) = *Inf4(i,j) + *Prob4(i,j);
					//*Sup4(i,j) = *Inf4(i,j) + *Prob3(i,j);
					
					//Done with those probabilities, begin to Move and Connect in one step.
					double RandNumber;
					int I, J;
					RandNumber = float( rand() ) / float( RAND_MAX);
					
					if(RandNumber >=*Inf0(i,j) && RandNumber < *Sup0(i,j))
					{
						
					}
					
					else if(RandNumber >=*Inf1(i,j) && RandNumber < *Sup1(i,j))
					{
						I = i + 1;
						J = j;
						
						
						if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
								SproutNumber -= 1;//comment this we have anasmosis
								
							}
							
							
							
							//*DirectionX(I, J) = 1;
							//*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
							//SproutNumber -=1;//Add this we have anasmosis
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;
							*SproutTips(i, j) = 0;	
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
							
							
							
							
						}
						else
						{
							
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionX(I, J) = 1;
							*Stability(i,j) = 1;
							*Stability(I,J) = 1;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
							
							
							
							
						}
						
						
						
						if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
						{
							*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorX(i,j)<-0.5)
							{*BloodVesselIndicatorX(i,j)=2;}
							else
							{*BloodVesselIndicatorX(i,j)=1;}
						}
						if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
						{
							*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorX(I,J)>0.5)
							{*BloodVesselIndicatorX(I,J)=2;}
							else
							{*BloodVesselIndicatorX(I,J)=-1;}
						}
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
						
						
					}
					
					else if(RandNumber >= *Inf2(i,j) && RandNumber < *Sup2(i,j))
					{
						I = i - 1;
						J = j;
						
						
						if(  (min_abs(*BloodVesselIndicator(I, J))) > 0 )
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; 
								SproutNumber -= 1;	
								
								
							}
							
							
							// *DirectionX(I, J) = -1;
							// *SproutTips(I, J) = 1;
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;	
							//SproutNumber -= 1;
							*SproutTips(i, j) = 0;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
						}
						else
						{
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob2 make Vessel Length"<<VesselListLength<<endl;
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionX(I, J) = -1;
							*Stability(i,j) = 1;
							*Stability(I,J) = 1;

							
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
							
							
						}
						
						
						if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
						{
							*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorX(i,j)>0.5)
							{*BloodVesselIndicatorX(i,j)=2;}
							else
							{*BloodVesselIndicatorX(i,j)=-1;}
						}
						if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
						{
							*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorX(I,J)<-0.5)
							{*BloodVesselIndicatorX(I,J)=2;}
							else
							{*BloodVesselIndicatorX(I,J)= 1;}
						}
						
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
					}
					
					else if(RandNumber >=*Inf3(i,j) && RandNumber < *Sup3(i,j))
					{
						I = i;
						J = j + 1;
						
						if( (*BloodVesselIndicator(I, J))> 0)
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; 
								SproutNumber -= 1;
								
								
							}
							
							
							
							// SproutNumber -= 1;
							// *DirectionY(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;	 
							*SproutTips(i, j) = 0;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
						}
						else
						{
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob3 make Vessel Length"<<VesselListLength<<endl;
							*Stability(i,j) = 1;
							*Stability(I,J) = 1;

							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionY(I, J) = 1;
							
							
							//cout<<"here"<<endl;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							//cout<<pCurrentVesselCell1->cIndex<<endl;
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//cout<<pCurrentVesselCell2->cIndex<<endl;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
								
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
						}
						
						if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
						{
							*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorY(i,j)<-0.5)
							{*BloodVesselIndicatorY(i,j)=2;}
							else
							{*BloodVesselIndicatorY(i,j)=1;}
						}
						if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
						{
							*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorY(I,J)>0.5)
							{*BloodVesselIndicatorY(I,J)=2;}
							else
							{*BloodVesselIndicatorY(I,J)=-1;}
						}
						
						
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
					}
					
					else if(RandNumber >= *Inf4(i,j) && RandNumber < *Sup4(i,j))
					{
						I = i;
						J = j - 1;
						
						
						if( (*BloodVesselIndicator(I, J)) > 0)
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; 
								SproutNumber -= 1;
							}
							
							
							// *DirectionY(I, J) = -1;
							// *SproutTips(I, J) = 1;
							//SproutNumber -= 1;
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;	
							*SproutTips(i, j) = 0;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
						}
						else
						{		
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob4 make Vessel Length"<<VesselListLength<<endl;
							*Stability(i,j) = 1;
							*Stability(I,J) = 1;

							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionY(I, J) = -1;
							
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
							
						}
						
						if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
						{
							*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorY(i,j)>0.5)
							{*BloodVesselIndicatorY(i,j)=2;}
							else
							{*BloodVesselIndicatorY(i,j)=-1;}
						}
						if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
						{
							*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorY(I,J)<-0.5)
							{*BloodVesselIndicatorY(I,J)=2;}
							else
							{*BloodVesselIndicatorY(I,J)=1;}
						}
						
						
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
					}
					
					//BOUNDARY SPROUTS We can ask it to be periodical?
				}//probtotal>0
				else
				{
					//cout<<"Show if i come here"<<endl;
					//getchar();
					*SproutTips(i,j) = 0;
				}//probtotal=0
				
			}//this is for the if sprout
			
			
			pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
			//cout<<"aftermove";
		}//this is for the loop
		pLastVesselPoint = pCurrentVesselPoint2;
		TheVessel->pLastVesselCell = pCurrentVesselCell2;
		//QuickPlot();// COMBINE
		cout <<"GrowSprout once"<<endl; 
		if(!growstill)
		{
			
			pCurrentVesselPoint1 = pFirstVesselPoint;
			
			
			
			while(pCurrentVesselPoint1)
			{
				pCurrentVesselPoint1->i_origin = pCurrentVesselPoint1->i;
				pCurrentVesselPoint1->j_origin = pCurrentVesselPoint1->j;
				
				
				
				pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
			}
			
			
			
			
			setorigin = 1;
			
			
			
			
			cout <<"origin reset"<<endl; 
			
		}//setorigin 
		
		
		
	}//still loop
	
	
	
	growstill=1;
	setorigin=0;
	LoopLength = VesselListLength; 
	
     pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
	 pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
	 pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
     pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
     pTempVesselCell = TheVessel->pFirstVesselCell;

	
	SproutTips = zeros(Xnodes,Ynodes);
	for(int j=12;j<=187;j=j+25)
	{
	int i=12;
	*SproutTips(i,j) = 1;
	
	}
	
	while(growstill || !setorigin)
	{
		
		cout<<"Once Begin"<<endl;
		growstill = 0;
		
		
		Prob0 = zeros(Xnodes, Ynodes);
		Prob1 = zeros(Xnodes, Ynodes);
		Prob2 = zeros(Xnodes, Ynodes);
		Prob3 = zeros(Xnodes, Ynodes);
		Prob4 = zeros(Xnodes, Ynodes);
		Inf0 = zeros(Xnodes, Ynodes);
		Inf1 = zeros(Xnodes, Ynodes);
		Inf2 = zeros(Xnodes, Ynodes);
		Inf3 = zeros(Xnodes, Ynodes);
		Inf4 = zeros(Xnodes, Ynodes);
		Sup0 = zeros(Xnodes, Ynodes);
		Sup1 = zeros(Xnodes, Ynodes);
		Sup2 = zeros(Xnodes, Ynodes);
		Sup3 = zeros(Xnodes, Ynodes);
		Sup4 = zeros(Xnodes, Ynodes);
		
		
		pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
		pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
		pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
		pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
		pTempVesselCell = TheVessel->pFirstVesselCell;
		LoopLength = VesselListLength;
		VesselBroke=0;
		
		
		for(int Loop = 0; Loop < LoopLength; Loop++)
		{
			//cout<<"enter loop"<<endl;
			int i = pCurrentVesselPoint1->i;
			int j = pCurrentVesselPoint1->j;
			int i_origin = pCurrentVesselPoint1->i_origin;
			int j_origin = pCurrentVesselPoint1->j_origin;
			
			
			
			if((i==0||j==0||i==Xnodes-1||j==Ynodes-1)&&*SproutTips(i,j)==1)
			{
				*SproutTips(i,j)=0;
				//SproutNumber -=1;
			}
			
			
			if( *SproutTips(i,j) > 0)
			{
				
				
				//cout<<"enter sprouttip loop"<<endl;
				//cout<<"beginsprout";
				if (i == 0) {iminus=1;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
				if (j == 0) {jminus=1;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
				if (i == Xnodes-1) {iplus = Xnodes - 2;} else  {iplus = i+1;}
				if (j == Ynodes-1) {jplus = Ynodes - 2;} else  {jplus = j+1;}
				
				//Prob0 should be with stability 
				
				/*
				 *Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i, j) );
				 
				 *Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i, j) );
				 
				 *Prob3(i, j) = Chemo_Response*( *TAF(i, jplus) - *TAF(i, j) );	   
				 
				 *Prob4(i, j) = Chemo_Response*( *TAF(i, jminus) - *TAF(i, j) );
				 */
				*Prob0(i, j) = 0;
				*Prob1(i, j) = 0;
				*Prob2(i, j) = 0;
				*Prob3(i, j) = 0;
				*Prob4(i, j) = 0;
				
				*Prob2(i,j) = 1;
				
				//the sprout shouldn't grow reverse  
				if (*BloodVesselIndicatorX(i,j) < 0 )
				{
					*Prob2(i,j) = 0;
				}
				else if (*BloodVesselIndicatorX(i,j) > 0)
				{
					*Prob1(i,j) = 0;
				}
				else if (*BloodVesselIndicatorY(i,j) < 0 )
				{
					*Prob4(i,j) = 0;
				}
				else if (*BloodVesselIndicatorY(i,j) > 0 )
				{
					*Prob3(i,j) = 0;
				}
				//other exceptions 
				if (i == Xnodes-1) 
				{ *Prob1(i, j) = 0.0; }
				else if (i == 0)   
				{ *Prob2(i, j) = 0.0; }
				if (j == ( Ynodes-1))
				{ *Prob3(i, j) = 0.0; }
				if (j == 0)
				{ *Prob4(i, j) = 0.0; }
				
				
				// cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
				//rescale the probabilities
				double ProbTotal = *Prob0(i,j) + *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j) ;
				if ( ProbTotal> 0 )
				{
					growstill = 1;
					
					*Prob0(i,j) /= ProbTotal;
					*Prob1(i,j) /= ProbTotal;
					*Prob2(i,j) /= ProbTotal;
					*Prob3(i,j) /= ProbTotal;
					*Prob4(i,j) /= ProbTotal;
					
					//cout<<*Prob0(i,j)<<"  "<<*Prob1(i,j)<<"  " <<*Prob2(i,j)<<"  "<<*Prob3(i,j)<<"  " <<*Prob4(i,j)<<endl;
					
					//Get Random Intervals
					*Inf0(i,j) = 0;
					*Sup0(i,j) = *Inf0(i,j) + *Prob0(i,j);
					//*Sup0(i,j) = *Inf0(i,j) + *Prob1(i,j);
					*Inf1(i,j) = *Sup0(i,j);
					*Sup1(i,j) = *Inf1(i,j) + *Prob1(i,j);
					//*Sup1(i,j) = *Inf1(i,j) + *Prob0(i,j);
					*Inf2(i,j) = *Sup1(i,j);
					*Sup2(i,j) = *Inf2(i,j) + *Prob2(i,j);
					*Inf3(i,j) = *Sup2(i,j);
					*Sup3(i,j) = *Inf3(i,j) + *Prob3(i,j);
					//*Sup3(i,j) = *Inf3(i,j) + *Prob4(i,j);
					*Inf4(i,j) = *Sup3(i,j);   //not neccesary, delete them to speed up the program
					*Sup4(i,j) = *Inf4(i,j) + *Prob4(i,j);
					//*Sup4(i,j) = *Inf4(i,j) + *Prob3(i,j);
					
					//Done with those probabilities, begin to Move and Connect in one step.
					double RandNumber;
					int I, J;
					RandNumber = float( rand() ) / float( RAND_MAX);
					
					if(RandNumber >=*Inf0(i,j) && RandNumber < *Sup0(i,j))
					{
						
					}
					
					else if(RandNumber >=*Inf1(i,j) && RandNumber < *Sup1(i,j))
					{
						I = i + 1;
						J = j;
						
						
						if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
								SproutNumber -= 1;//comment this we have anasmosis
								
							}
							
							
							
							//*DirectionX(I, J) = 1;
							//*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
							//SproutNumber -=1;//Add this we have anasmosis
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;
							*SproutTips(i, j) = 0;	
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
							
							
							
							
						}
						else
						{
							
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionX(I, J) = 1;
							*Stability(i,j) = 1;
							*Stability(I,J) = 1;

							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
							
							
							
							
						}
						
						
						
						if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
						{
							*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorX(i,j)<-0.5)
							{*BloodVesselIndicatorX(i,j)=2;}
							else
							{*BloodVesselIndicatorX(i,j)=1;}
						}
						if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
						{
							*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorX(I,J)>0.5)
							{*BloodVesselIndicatorX(I,J)=2;}
							else
							{*BloodVesselIndicatorX(I,J)=-1;}
						}
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
						
						
					}
					
					else if(RandNumber >= *Inf2(i,j) && RandNumber < *Sup2(i,j))
					{
						I = i - 1;
						J = j;
						
						
						if(  (min_abs(*BloodVesselIndicator(I, J))) > 0 )
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; 
								SproutNumber -= 1;	
								
								
							}
							
							
							// *DirectionX(I, J) = -1;
							// *SproutTips(I, J) = 1;
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;	
							//SproutNumber -= 1;
							*SproutTips(i, j) = 0;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
						}
						else
						{
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob2 make Vessel Length"<<VesselListLength<<endl;
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionX(I, J) = -1;
							*Stability(i,j) = 1;
							*Stability(I,J) = 1;

							
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
							
							
						}
						
						
						if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
						{
							*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorX(i,j)>0.5)
							{*BloodVesselIndicatorX(i,j)=2;}
							else
							{*BloodVesselIndicatorX(i,j)=-1;}
						}
						if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
						{
							*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorX(I,J)<-0.5)
							{*BloodVesselIndicatorX(I,J)=2;}
							else
							{*BloodVesselIndicatorX(I,J)= 1;}
						}
						
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
					}
					
					else if(RandNumber >=*Inf3(i,j) && RandNumber < *Sup3(i,j))
					{
						I = i;
						J = j + 1;
						
						if( (*BloodVesselIndicator(I, J))> 0)
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; 
								SproutNumber -= 1;
								
								
							}
							
							
							
							// SproutNumber -= 1;
							// *DirectionY(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;	 
							*SproutTips(i, j) = 0;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
						}
						else
						{
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob3 make Vessel Length"<<VesselListLength<<endl;
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionY(I, J) = 1;
							*Stability(i,j) = 1;
							*Stability(I,J) = 1;

							
							//cout<<"here"<<endl;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							//cout<<pCurrentVesselCell1->cIndex<<endl;
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//cout<<pCurrentVesselCell2->cIndex<<endl;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
								
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
						}
						
						if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
						{
							*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorY(i,j)<-0.5)
							{*BloodVesselIndicatorY(i,j)=2;}
							else
							{*BloodVesselIndicatorY(i,j)=1;}
						}
						if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
						{
							*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorY(I,J)>0.5)
							{*BloodVesselIndicatorY(I,J)=2;}
							else
							{*BloodVesselIndicatorY(I,J)=-1;}
						}
						
						
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
					}
					
					else if(RandNumber >= *Inf4(i,j) && RandNumber < *Sup4(i,j))
					{
						I = i;
						J = j - 1;
						
						
						if( (*BloodVesselIndicator(I, J)) > 0)
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; 
								SproutNumber -= 1;
							}
							
							
							// *DirectionY(I, J) = -1;
							// *SproutTips(I, J) = 1;
							//SproutNumber -= 1;
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;	
							*SproutTips(i, j) = 0;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
						}
						else
						{		
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob4 make Vessel Length"<<VesselListLength<<endl;
							
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionY(I, J) = -1;
							*Stability(i,j) = 1;
							*Stability(I,J) = 1;

							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
							
						}
						
						if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
						{
							*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorY(i,j)>0.5)
							{*BloodVesselIndicatorY(i,j)=2;}
							else
							{*BloodVesselIndicatorY(i,j)=-1;}
						}
						if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
						{
							*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorY(I,J)<-0.5)
							{*BloodVesselIndicatorY(I,J)=2;}
							else
							{*BloodVesselIndicatorY(I,J)=1;}
						}
						
						
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
					}
					
					//BOUNDARY SPROUTS We can ask it to be periodical?
				}//probtotal>0
				else
				{
					//cout<<"Show if i come here"<<endl;
					//getchar();
					*SproutTips(i,j) = 0;
				}//probtotal=0
				
			}//this is for the if sprout
			
			
			pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
			//cout<<"aftermove";
		}//this is for the loop
		pLastVesselPoint = pCurrentVesselPoint2;
		TheVessel->pLastVesselCell = pCurrentVesselCell2;
		//QuickPlot();// COMBINE
		cout <<"GrowSprout once"<<endl; 
		if(!growstill)
		{
			
			pCurrentVesselPoint1 = pFirstVesselPoint;
			
			
			
			while(pCurrentVesselPoint1)
			{
				pCurrentVesselPoint1->i_origin = pCurrentVesselPoint1->i;
				pCurrentVesselPoint1->j_origin = pCurrentVesselPoint1->j;
				
				
				
				pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
			}
			
			
			
			
			setorigin = 1;
			
			
			
			
			cout <<"origin reset"<<endl; 
			
		}//setorigin 
		
		
		
	}//still loop
	
 pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
      while(pCurrentVesselPoint1)
			{
			    int i = pCurrentVesselPoint1-> i;
				int j = pCurrentVesselPoint1-> j;
				*PreExist(i,j) = 1;
			
				
				pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
			}
	cout <<"function GrowSprout finish"<<endl;
}

void VesselStructure::GrowSprout_Step(CellVesselStructure* TheVessel,int Step)
{
cout <<"function GrowSprout running"<<endl; 
extern bool VesselBroke;
extern int touchpoints;
extern Matrix SproutTips;
extern Matrix Stability;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
double Stable_down = RetrieveDoubleParameter( ParameterFile , "Stable_down");
extern Matrix TAF;
extern Matrix ECM;
extern Matrix CellColor;
extern int Xnodes, Ynodes;
double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
double String = RetrieveDoubleParameter( ParameterFile , "String");
double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
AngioDeltaY = 1.0 /Ynodes;
Matrix Prob0 = zeros(Xnodes, Ynodes);
Matrix Prob1 = zeros(Xnodes, Ynodes);
Matrix Prob2 = zeros(Xnodes, Ynodes);
Matrix Prob3 = zeros(Xnodes, Ynodes);
Matrix Prob4 = zeros(Xnodes, Ynodes);
Matrix Inf0 = zeros(Xnodes, Ynodes);
Matrix Inf1 = zeros(Xnodes, Ynodes);
Matrix Inf2 = zeros(Xnodes, Ynodes);
Matrix Inf3 = zeros(Xnodes, Ynodes);
Matrix Inf4 = zeros(Xnodes, Ynodes);
Matrix Sup0 = zeros(Xnodes, Ynodes);
Matrix Sup1 = zeros(Xnodes, Ynodes);
Matrix Sup2 = zeros(Xnodes, Ynodes);
Matrix Sup3 = zeros(Xnodes, Ynodes);
Matrix Sup4 = zeros(Xnodes, Ynodes);
int iminus,iplus,jminus,jplus;
int growstill=0;
bool setorigin=0;
int LoopLength = VesselListLength; 
 cout<<"Growth Begin"<<endl;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
VesselCell* pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
VesselCell* pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
VesselCell* pTempVesselCell = TheVessel->pFirstVesselCell;

while(growstill<Step)
{

cout<<"Once Begin"<<endl;
growstill++;


 Prob0 = zeros(Xnodes, Ynodes);
 Prob1 = zeros(Xnodes, Ynodes);
 Prob2 = zeros(Xnodes, Ynodes);
 Prob3 = zeros(Xnodes, Ynodes);
 Prob4 = zeros(Xnodes, Ynodes);
 Inf0 = zeros(Xnodes, Ynodes);
 Inf1 = zeros(Xnodes, Ynodes);
 Inf2 = zeros(Xnodes, Ynodes);
 Inf3 = zeros(Xnodes, Ynodes);
 Inf4 = zeros(Xnodes, Ynodes);
 Sup0 = zeros(Xnodes, Ynodes);
 Sup1 = zeros(Xnodes, Ynodes);
 Sup2 = zeros(Xnodes, Ynodes);
 Sup3 = zeros(Xnodes, Ynodes);
 Sup4 = zeros(Xnodes, Ynodes);


pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
pTempVesselCell = TheVessel->pFirstVesselCell;
LoopLength = VesselListLength;
VesselBroke=0;


for(int Loop = 0; Loop < LoopLength; Loop++)
{
   //cout<<"enter loop"<<endl;
	int i = pCurrentVesselPoint1->i;
	int j = pCurrentVesselPoint1->j;
	int i_origin = pCurrentVesselPoint1->i_origin;
	int j_origin = pCurrentVesselPoint1->j_origin;

	
	
if((i==0||j==0||i==Xnodes-1||j==Ynodes-1)&&*SproutTips(i,j)==1)
	{
	*SproutTips(i,j)=0;
	//SproutNumber -=1;
	}
	
	
	if( *SproutTips(i,j) > 0)
	{
	
	
	//cout<<"enter sprouttip loop"<<endl;
		//cout<<"beginsprout";
	    if (i == 0) {iminus=1;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=1;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = Xnodes - 2;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = Ynodes - 2;} else  {jplus = j+1;}
		
	 		//Prob0 should be with stability 
													
	  /*
		  *Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i, j) );
	 
	      *Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i, j) );
	
	      *Prob3(i, j) = Chemo_Response*( *TAF(i, jplus) - *TAF(i, j) );	   
    
	      *Prob4(i, j) = Chemo_Response*( *TAF(i, jminus) - *TAF(i, j) );
		  
	 */
	 
		*Prob0(i, j) = 0;
		*Prob1(i, j) = 0;
		*Prob2(i, j) = 0;
		*Prob3(i, j) = 0;
		*Prob4(i, j) = 0;
		if( *TAF(iplus, j) - *TAF(i, j) > 0.0)
		{
		  *Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i_origin, j_origin) ) - String*pow((iplus-i_origin)*(iplus-i_origin)+(j-j_origin)*(j-j_origin), 0.5);
	  // cout<< "*Prob1(i, j)"<<*Prob1(i, j)<<endl;
		}
		if(*TAF(iminus, j) - *TAF(i, j) > 0.0)
		{
	      *Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i_origin, j_origin) ) - String*pow((iminus-i_origin)*(iminus-i_origin)+(j-j_origin)*(j-j_origin), 0.5);
	//	  cout<< "*Prob2(i, j)"<<*Prob2(i, j)<<endl;
		}
		if(*TAF(i, jplus) - *TAF(i, j) > 0.0)
		{
	//	cout<< "*Prob3(i, j)"<<*Prob3(i, j)<<endl;
	      *Prob3(i, j) = Chemo_Response*( *TAF(i,jplus) - *TAF(i_origin, j_origin) )  - String*pow((i-i_origin)*(i-i_origin)+(jplus-j_origin)*(jplus-j_origin), 0.5);	   
		}
		if(*TAF(i, jminus) - *TAF(i, j) > 0.0)
		{
		
	//	cout<< "*Prob4(i, j)"<<*Prob4(i, j)<<endl;
	      *Prob4(i, j) = Chemo_Response*(  *TAF(i,jminus) - *TAF(i_origin, j_origin) ) - String*pow((i-i_origin)*(i-i_origin)+(jminus-j_origin)*(jminus-j_origin), 0.5);
	    }
		  
		  
		  
		
			
		if( *Prob0(i,j) <= 0 )
		{
		*Prob0(i,j) = 0;
		}
		if( *Prob1(i,j) <= 0 )
		{
		*Prob1(i,j) = 0;
	
		}
        if( *Prob2(i,j) <= 0 )
		{
		*Prob2(i,j) = 0;
	
		}
		if( *Prob3(i,j) <= 0 )
		{
		*Prob3(i,j) = 0;
	
		}

		if( *Prob4(i,j) <= 0 )
		{
		*Prob4(i,j) = 0;
	
		}

       
	   
	   
	   
	 //the sprout shouldn't grow reverse  
	   if (*BloodVesselIndicatorX(i,j) < 0 )
	   {
	   *Prob2(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorX(i,j) > 0)
	   {
	   *Prob1(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) < 0 )
	   {
	   *Prob4(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) > 0 )
	   {
	   *Prob3(i,j) = 0;
	   }
	   //other exceptions 
		if (i == Xnodes-1) 
	    { *Prob1(i, j) = 0.0; }
		else if (i == 0)   
	    { *Prob2(i, j) = 0.0; }
		if (j == ( Ynodes-1))
	    { *Prob3(i, j) = 0.0; }
		 if (j == 0)
	    { *Prob4(i, j) = 0.0; }


   // cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
     //rescale the probabilities
	 double ProbTotal = *Prob0(i,j) + *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j) ;
	 if ( ProbTotal> 0 )
	 {
		//growstill = 1;
		
		*Prob0(i,j) /= ProbTotal;
		*Prob1(i,j) /= ProbTotal;
		*Prob2(i,j) /= ProbTotal;
		*Prob3(i,j) /= ProbTotal;
		*Prob4(i,j) /= ProbTotal;
	 
	// cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
	 
	 //Get Random Intervals
	 *Inf0(i,j) = 0;
	 *Sup0(i,j) = *Inf0(i,j) + *Prob0(i,j);
	 //*Sup0(i,j) = *Inf0(i,j) + *Prob1(i,j);
	 *Inf1(i,j) = *Sup0(i,j);
	 *Sup1(i,j) = *Inf1(i,j) + *Prob1(i,j);
	 //*Sup1(i,j) = *Inf1(i,j) + *Prob0(i,j);
	 *Inf2(i,j) = *Sup1(i,j);
	 *Sup2(i,j) = *Inf2(i,j) + *Prob2(i,j);
	 *Inf3(i,j) = *Sup2(i,j);
	 *Sup3(i,j) = *Inf3(i,j) + *Prob3(i,j);
	 //*Sup3(i,j) = *Inf3(i,j) + *Prob4(i,j);
	 *Inf4(i,j) = *Sup3(i,j);   //not neccesary, delete them to speed up the program
	 *Sup4(i,j) = *Inf4(i,j) + *Prob4(i,j);
	 //*Sup4(i,j) = *Inf4(i,j) + *Prob3(i,j);
	 
	 //Done with those probabilities, begin to Move and Connect in one step.
 double RandNumber;
	 int I, J;
	 RandNumber = float( rand() ) / float( RAND_MAX);

	 if(RandNumber >=*Inf0(i,j) && RandNumber < *Sup0(i,j))
	 {
		
	 }
	 
	 else if(RandNumber >=*Inf1(i,j) && RandNumber < *Sup1(i,j))
	 {
		I = i + 1;
		J = j;
	
	
		if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
		
		
		if(*Stability(I,J)>0)
		{
           *Stability(I,J) = *Stability(I,J) - AngioDeltaT * Stable_down;
		   if(*Stability(I,J) < 0)
          {
			*Stability(I,J) = 0;
		  }
		  /****/
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
			SproutNumber -= 1;//comment this we have anasmosis
			
		  }
		  
		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;
		 *SproutTips(i, j) = 0;	
		 
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pCurrentVesselCell1->cFate = 'S';
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 pTempVesselCell->cFate = 'S';
		 Connect(pCurrentVesselCell1,pTempVesselCell);
		 
		/***/
		  
		
		}
		else
		{
	     if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
			SproutNumber -= 1;//comment this we have anasmosis
			
		  }
		  
		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;
		 *SproutTips(i, j) = 0;	
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		pCurrentVesselCell1->cFate = 'S';
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 pTempVesselCell->cFate = 'S';
		 Connect(pCurrentVesselCell1,pTempVesselCell);
		 
		} 
		   
			
			 
			   
		}
		else
		{
		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->i_origin = i_origin;
		pCurrentVesselPoint2->j_origin = j_origin;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionX(I, J) = 1;
		
		pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		
		double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
		double sezi;
		sezi= float( rand() ) / float( RAND_MAX);
		if(sezi<Prob)
		{
		
		TheVessel->CellListLength++;
		VesselCell* pNewVesselCell = new VesselCell;
	    pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
	    pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
		pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
		pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
		pCurrentVesselCell2->cCover = 1;
		pCurrentVesselCell2->i[0] = I;
		pCurrentVesselCell2->j[0] = J;
		pCurrentVesselCell2->pNextVesselCell = NULL;
		//pCurrentVesselCell2->cConnection = 1;
	    //pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
		Connect(pCurrentVesselCell2,pCurrentVesselCell1);
		*CellColor(I,J) = 100;
		pCurrentVesselCell1->cFate = 'S';
		pCurrentVesselCell2->cFate = 'T';	
		
		}//mitosis and grow
		else
		{
		
		pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
		pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
		pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
		
		*CellColor(I,J) = 100;
		if(pCurrentVesselCell1->cCover==pCurrentVesselCell1->coverlimit)
		{
		*SproutTips(I,J)=0;
		pCurrentVesselCell1->cFate = 'S';
		
		}
		
		}//justgrow
		
		
		
		
		
		}
		
	
		
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
		
		
	 }
     
	 else if(RandNumber >= *Inf2(i,j) && RandNumber < *Sup2(i,j))
	 {
		I = i - 1;
		J = j;
	
		
		if(  (min_abs(*BloodVesselIndicator(I, J))) > 0 )
		{
		
		
		if(*Stability(I,J)>0)
		{
           *Stability(I,J) = *Stability(I,J) - AngioDeltaT * Stable_down;
		   if(*Stability(I,J) < 0)
          {
			*Stability(I,J) = 0;
		  }
		  
		   /****/
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
			SproutNumber -= 1;//comment this we have anasmosis
			
		  }
		  
		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;
		 *SproutTips(i, j) = 0;	
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pCurrentVesselCell1->cFate = 'S';
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 pTempVesselCell->cFate = 'S';
		 
		 Connect(pCurrentVesselCell1,pTempVesselCell);
		/***/
		  
		
		}
		else
		{
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
			SproutNumber -= 1;	
			
	
		  }
		 
		  		  
		// *DirectionX(I, J) = -1;
		// *SproutTips(I, J) = 1;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	
		 //SproutNumber -= 1;
		 *SproutTips(i, j) = 0;
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		pCurrentVesselCell1->cFate = 'S';
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 pTempVesselCell->cFate = 'S';
		 Connect(pCurrentVesselCell1,pTempVesselCell);
	      }
	 
		}
		else
		{
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->i_origin = i_origin;
		pCurrentVesselPoint2->j_origin = j_origin;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob2 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		//*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionX(I, J) = -1;
		
		
		pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		
		double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
		double sezi;
		sezi= float( rand() ) / float( RAND_MAX);
		if(sezi<Prob)
		{
		
		TheVessel->CellListLength++;
		VesselCell* pNewVesselCell = new VesselCell;
	    pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
	    pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
		pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
		pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
		pCurrentVesselCell2->cCover = 1;
		pCurrentVesselCell2->i[0] = I;
		pCurrentVesselCell2->j[0] = J;
		pCurrentVesselCell2->pNextVesselCell = NULL;
		//pCurrentVesselCell2->cConnection = 1;
	    //pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
		Connect(pCurrentVesselCell2,pCurrentVesselCell1);
		*CellColor(I,J) = 100;
		pCurrentVesselCell1->cFate = 'S';
		pCurrentVesselCell2->cFate = 'T';	
		
		}//mitosis and grow
		else
		{
		
		pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
		pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
		pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
		
		*CellColor(I,J) = 100;
		if(pCurrentVesselCell1->cCover==pCurrentVesselCell1->coverlimit)
		{
		*SproutTips(I,J)=0;
		pCurrentVesselCell1->cFate = 'S';
		}
		
		}//justgrow
		
		
		
		}
		
	
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }
	 
	 else if(RandNumber >=*Inf3(i,j) && RandNumber < *Sup3(i,j))
	 {
		I = i;
		J = j + 1;
	
		if( (*BloodVesselIndicator(I, J))> 0)
		{
		 
		 if(*Stability(I,J)>0)
		{
           *Stability(I,J) = *Stability(I,J) - AngioDeltaT * Stable_down;
		   if(*Stability(I,J) < 0)
          {
			*Stability(I,J) = 0;
		  }
		  /****/
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
			SproutNumber -= 1;//comment this we have anasmosis
			
		  }
		  
		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;
		 *SproutTips(i, j) = 0;	
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pCurrentVesselCell1->cFate = 'S';
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 pTempVesselCell->cFate = 'S';
		 Connect(pCurrentVesselCell1,pTempVesselCell);
		/***/
		   
		  
		
		}
		else
		{
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
				SproutNumber -= 1;
				  

		  }
		 
			  
		
		// SproutNumber -= 1;
		// *DirectionY(I, J) = 1;
		 //*SproutTips(I, J) = 1;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	 
		 *SproutTips(i, j) = 0;
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pCurrentVesselCell1->cFate = 'S';
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 pTempVesselCell->cFate = 'S';
		 Connect(pCurrentVesselCell1,pTempVesselCell);
		}
		 
		}
		else
		{
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->i_origin = i_origin;
		pCurrentVesselPoint2->j_origin = j_origin;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob3 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		//*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionY(I, J) = 1;
		
		
		//cout<<"here"<<endl;
		pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		//cout<<pCurrentVesselCell1->cIndex<<endl;
		
		double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
		double sezi;
		sezi= float( rand() ) / float( RAND_MAX);
		if(sezi<Prob)
		{
		
		TheVessel->CellListLength++;
		VesselCell* pNewVesselCell = new VesselCell;
	    pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
	    pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
		pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
		pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
		pCurrentVesselCell2->cCover = 1;
		pCurrentVesselCell2->i[0] = I;
		pCurrentVesselCell2->j[0] = J;
		pCurrentVesselCell2->pNextVesselCell = NULL;
		//cout<<pCurrentVesselCell2->cIndex<<endl;
		//pCurrentVesselCell2->cConnection = 1;
	    //pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
		
		Connect(pCurrentVesselCell2,pCurrentVesselCell1);
		*CellColor(I,J) = 100;

		pCurrentVesselCell1->cFate = 'S';
		pCurrentVesselCell2->cFate = 'T';	
		
		}//mitosis and grow
		else
		{
		
		pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
		pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
		pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
		
		*CellColor(I,J) = 100;
		if(pCurrentVesselCell1->cCover==pCurrentVesselCell1->coverlimit)
		{
		*SproutTips(I,J)=0;
		pCurrentVesselCell1->cFate = 'S';
		}
		
		}//justgrow
		
		}
	
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }
	 
	 else if(RandNumber >= *Inf4(i,j) && RandNumber < *Sup4(i,j))
	 {
		I = i;
		J = j - 1;
	
				
		if( (*BloodVesselIndicator(I, J)) > 0)
		{
		
		if(*Stability(I,J)>0)
		{
           *Stability(I,J) = *Stability(I,J) - AngioDeltaT * Stable_down;
		   if(*Stability(I,J) < 0)
          {
			*Stability(I,J) = 0;
		  }
		   /****/
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
			SproutNumber -= 1;//comment this we have anasmosis
			
		  }
		  
		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;
		 *SproutTips(i, j) = 0;	
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pCurrentVesselCell1->cFate = 'S';
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 pTempVesselCell->cFate = 'S';
		 Connect(pCurrentVesselCell1,pTempVesselCell);
		/***/
		  
		  
		
		}
		else
		{
		 
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
			 SproutNumber -= 1;
		  }
		 		  
		 
		// *DirectionY(I, J) = -1;
		// *SproutTips(I, J) = 1;
		 //SproutNumber -= 1;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	
		 *SproutTips(i, j) = 0;
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pCurrentVesselCell1->cFate = 'S';
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 pTempVesselCell->cFate = 'S';
		 Connect(pCurrentVesselCell1,pTempVesselCell);

		}
		 
		}
		else
		{		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->i_origin = i_origin;
		pCurrentVesselPoint2->j_origin = j_origin;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob4 make Vessel Length"<<VesselListLength<<endl;

		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		//*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionY(I, J) = -1;
		
		pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		
		double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
		double sezi;
		sezi= float( rand() ) / float( RAND_MAX);
		if(sezi<Prob)
		{
		
		TheVessel->CellListLength++;
		VesselCell* pNewVesselCell = new VesselCell;
	    pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
	    pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
		pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
		pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
		pCurrentVesselCell2->cCover = 1;
		pCurrentVesselCell2->i[0] = I;
		pCurrentVesselCell2->j[0] = J;
		pCurrentVesselCell2->pNextVesselCell = NULL;
		//pCurrentVesselCell2->cConnection = 1;
	    //pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
		Connect(pCurrentVesselCell2,pCurrentVesselCell1);
		*CellColor(I,J) = 100;
		
		pCurrentVesselCell1->cFate = 'S';
		pCurrentVesselCell2->cFate = 'T';	
		}//mitosis and grow
		else
		{
		
		pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
		pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
		pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
		
		*CellColor(I,J) = 100;
		if(pCurrentVesselCell1->cCover==pCurrentVesselCell1->coverlimit)
		{
		*SproutTips(I,J)=0;
		pCurrentVesselCell1->cFate = 'S';
		}
		
		}//justgrow
		
		
		}
	
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }

	//BOUNDARY SPROUTS We can ask it to be periodical?
    }//probtotal>0
	else
	{
	*SproutTips(i,j) = 0;
	pCurrentVesselCell1 = TheVessel->FindCell(i,j);

	pCurrentVesselCell1->cFate = 'S';
	}//probtotal=0
	
	}//this is for the if sprout
		
	
	pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	 //cout<<"aftermove";
    }//this is for the loop
   pLastVesselPoint = pCurrentVesselPoint2;
   TheVessel->pLastVesselCell = pCurrentVesselCell2;
   //QuickPlot();// COMBINE
   cout <<"GrowSprout once"<<endl; 
   if(growstill==Step)
   {
   
   pCurrentVesselPoint1 = pFirstVesselPoint;
   
   
   
   while(pCurrentVesselPoint1)
	{
		 pCurrentVesselPoint1->i_origin = pCurrentVesselPoint1->i;
		 pCurrentVesselPoint1->j_origin = pCurrentVesselPoint1->j;
		
	
			  
	  pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	}

   
   
   
   setorigin = 1;
   
   
   
   
   cout <<"origin reset"<<endl; 
   
   }//setorigin 
   
   
   
   }//still loop
   cout <<"function GrowSprout finish"<<endl;
 }

void MoveMCell(double Time, double Duration)
{
//cout <<"function GrowSprout running"<<endl; 
extern Matrix BloodVesselIndicator;
extern Matrix Oxygen;
extern Matrix Pressure;
extern Matrix TracerT;
extern Matrix Macrof;
extern Matrix PreExist;
extern Matrix TAF;
extern double t;
extern int Xnodes, Ynodes;
double MExtra = RetrieveDoubleParameter( ParameterFile , "MExtra");
double MDie = RetrieveDoubleParameter( ParameterFile , "MDie");
double M_O = RetrieveDoubleParameter( ParameterFile , "M_O");
double M_P = RetrieveDoubleParameter( ParameterFile , "M_P");
//sweep the list to update rather than matrix?
//yes!
//should we have more list?
//we don't need to, because even if we have two main vessel,
//it's like a whole system with the general BC and IC.

//DON'T FORGET THE INDICATOR
AngioDeltaX = 1.0;// here...I don't agree Min's Note
AngioDeltaY = 1.0;
Matrix Prob0 = zeros(Xnodes, Ynodes);
Matrix Prob1 = zeros(Xnodes, Ynodes);
Matrix Prob2 = zeros(Xnodes, Ynodes);
Matrix Prob3 = zeros(Xnodes, Ynodes);
Matrix Prob4 = zeros(Xnodes, Ynodes);
Matrix Inf0 = zeros(Xnodes, Ynodes);
Matrix Inf1 = zeros(Xnodes, Ynodes);
Matrix Inf2 = zeros(Xnodes, Ynodes);
Matrix Inf3 = zeros(Xnodes, Ynodes);
Matrix Inf4 = zeros(Xnodes, Ynodes);
Matrix Sup0 = zeros(Xnodes, Ynodes);
Matrix Sup1 = zeros(Xnodes, Ynodes);
Matrix Sup2 = zeros(Xnodes, Ynodes);
Matrix Sup3 = zeros(Xnodes, Ynodes);
Matrix Sup4 = zeros(Xnodes, Ynodes);
int iminus,iplus,jminus,jplus;


for(int i = 0; i< Xnodes; i++)
{
for(int j = 0; j< Ynodes; j++)
{

if(*Macrof(i,j)==0)
{
if(*BloodVesselIndicator(i,j)>0&&*PreExist(i,j) ==0)
{
 double RandNumber1;
	 RandNumber1 = float( rand() ) / float( RAND_MAX);
	 if(RandNumber1< (AngioDeltaT*MExtra* *TAF(i,j)))
	 {
	 if(t>=Time&&t<(Time+Duration))
	 {
	 *Macrof(i,j) = 1;
	 *TracerT(i,j) = 1;
	// cout<<"here"<<endl;
	 //getchar();
	 }
	 else
	 {
	 *Macrof(i,j) = 1;
	 *TracerT(i,j) = 0;
	 }
	 
	 }
}

}
else
{   
 double RandNumber2;
	 RandNumber2 = float( rand() ) / float( RAND_MAX);
	 if(RandNumber2< AngioDeltaT*MDie)
	 {
	 *TracerT(i,j) = 0;
	 *Macrof(i,j) = 0;
	 }
	else
	{
	//cout<<"enter sprouttip loop"<<endl;
	    if (i == 0) {iminus=1;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=1;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = Xnodes - 2;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = Ynodes - 2;} else  {jplus = j+1;}
	                 											 
	  /*
		  *Prob1(i, j) = -( AngioDeltaT / ( 2.0 * pow( AngioDeltaX, 2 ) ) )
		                   *(M_O*( *Oxygen(iplus, j) - *Oxygen(i, j) )
		                      + M_P * ( *Pressure(iplus, j) - *Pressure(i, j) ) );
	 
	
	      *Prob2(i, j) = -( AngioDeltaT / ( 2.0 * pow( AngioDeltaX, 2 ) ) )
		                   *(M_O*( *Oxygen(iminus, j) - *Oxygen(i, j) )
		                      + M_P * ( *Pressure(minus, j) - *Pressure(i, j) ) );	
	
	
	     *Prob3(i, j) =  -( AngioDeltaT / ( 2.0 * pow( AngioDeltaY, 2 ) ) )
		                   *(M_O*( *Oxygen(i, jplus) - *Oxygen(i, j) )
		                      + M_P * ( *Pressure(i, jplus) - *Pressure(i, j) ) );
	
	   
    
	     *Prob4(i, j) = -( AngioDeltaT / ( 2.0 * pow( AngioDeltaY, 2 ) ) )
		                   *(M_O*( *Oxygen(i, jminus) - *Oxygen(i, j) )
		                      + M_P * ( *Pressure(i, jminus) - *Pressure(i, j) ) );
		
		 *Prob0(i, j) = 1.0+ *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j);	  
		
		*/
											
        *Prob1(i, j) = -( AngioDeltaT / ( 2.0 * pow( AngioDeltaX, 2 ) ) )
		                   *(M_O*( *Oxygen(iplus, j) - *Oxygen(i, j) )
		                      + M_P * ( *Pressure(iplus, j) - *Pressure(i, j) ) );
	 
	
	      *Prob2(i, j) = -( AngioDeltaT / ( 2.0 * pow( AngioDeltaX, 2 ) ) )
		                   *(M_O*( *Oxygen(iminus, j) - *Oxygen(i, j) )
							+ M_P * ( *Pressure(iminus, j) - *Pressure(i, j) ) );	
	
	
	     *Prob3(i, j) =  -( AngioDeltaT / ( 2.0 * pow( AngioDeltaY, 2 ) ) )
		                   *(M_O*( *Oxygen(i, jplus) - *Oxygen(i, j) )
							+ M_P * ( *Pressure(i, jplus) - *Pressure(i, j) ) );
	
	   
    
	     *Prob4(i, j) = -( AngioDeltaT / ( 2.0 * pow( AngioDeltaY, 2 ) ) )
		                   *(M_O*( *Oxygen(i, jminus) - *Oxygen(i, j) )
							+ M_P * ( *Pressure(i, jminus) - *Pressure(i, j) ) );
		
		 *Prob0(i, j) = 1.0;	
		  

   //cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
	 // make probabilities above 0  

			
		if( *Prob0(i,j) < 0 )
		{
		*Prob0(i,j) = 0;
		}
		if( *Prob1(i,j) < 0 )
		{
		*Prob1(i,j) = 0;
		}
		if( *Prob2(i,j) < 0 )
		{
		*Prob2(i,j) = 0;		
		}
		if( *Prob3(i,j) < 0 )
		{
		*Prob3(i,j) = 0;		
		}
		if( *Prob4(i,j) < 0 )
		{
		*Prob4(i,j) = 0;		
		}
       
	  	   
	 //the sprout shouldn't grow reverse  

	   //other exceptions 
		if (i == Xnodes-1) 
	    { *Prob1(i, j) = 0.0; }
		else if (i == 0)   
	    { *Prob2(i, j) = 0.0; }
		if (j ==Ynodes-1)
	    { *Prob3(i, j) = 0.0; }
		 if (j == 0)
	    { *Prob4(i, j) = 0.0; }


   // cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
     //rescale the probabilities
	 double ProbTotal = *Prob0(i,j) + *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j) ;
	 if ( ProbTotal> 0 )
	 {
		*Prob0(i,j) /= ProbTotal;
		*Prob1(i,j) /= ProbTotal;
		*Prob2(i,j) /= ProbTotal;
		*Prob3(i,j) /= ProbTotal;
		*Prob4(i,j) /= ProbTotal;
	 }
	 cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
	 
	 //Get Random Intervals
	 *Inf0(i,j) = 0;
	 *Sup0(i,j) = *Inf0(i,j) + *Prob0(i,j);
	 //*Sup0(i,j) = *Inf0(i,j) + *Prob1(i,j);
	 *Inf1(i,j) = *Sup0(i,j);
	 *Sup1(i,j) = *Inf1(i,j) + *Prob1(i,j);
	 //*Sup1(i,j) = *Inf1(i,j) + *Prob0(i,j);
	 *Inf2(i,j) = *Sup1(i,j);
	 *Sup2(i,j) = *Inf2(i,j) + *Prob2(i,j);
	 *Inf3(i,j) = *Sup2(i,j);
	 *Sup3(i,j) = *Inf3(i,j) + *Prob3(i,j);
	 //*Sup3(i,j) = *Inf3(i,j) + *Prob4(i,j);
	 *Inf4(i,j) = *Sup3(i,j);   //not neccesary, delete them to speed up the program
	 *Sup4(i,j) = *Inf4(i,j) + *Prob4(i,j);
	 //*Sup4(i,j) = *Inf4(i,j) + *Prob3(i,j);
	 
	 //Done with those probabilities, begin to Move and Connect in one step.
	 
	 double RandNumber;
	 int I, J;
	 RandNumber = float( rand() ) / float( RAND_MAX);
	 if(RandNumber >=*Inf0(i,j) && RandNumber < *Sup0(i,j))
	 {
		I = i;
		J = j;
		
	 }
	 
	 else if(RandNumber >=*Inf1(i,j) && RandNumber < *Sup1(i,j))
	 {
		I = i + 1;
		J = j;
		
		if(*Macrof(I, J)==0)//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
		*Macrof(I, J) = 1;
		*Macrof(i, j) = 0;
		*TracerT(I, J) = *TracerT(i, j);
		*TracerT(i, j) = 0;
		}

		
	 }
     
	 else if(RandNumber >= *Inf2(i,j) && RandNumber < *Sup2(i,j))
	 {
		I = i - 1;
		J = j;
		
		if(*Macrof(I, J)==0)//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
		*Macrof(I, J) = 1;
		*Macrof(i, j) = 0;
		*TracerT(I, J) = *TracerT(i, j);
		*TracerT(i, j) = 0;
		}
	 }
	 
	 else if(RandNumber >=*Inf3(i,j) && RandNumber < *Sup3(i,j))
	 {
		I = i;
		J = j + 1;
		
		
		if(*Macrof(I, J)==0)//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
		*Macrof(I, J) = 1;
		*Macrof(i, j) = 0;
		*TracerT(I, J) = *TracerT(i, j);
		*TracerT(i, j) = 0;
		}
	 }
	 
	 else if(RandNumber >= *Inf4(i,j) && RandNumber < *Sup4(i,j))
	 {
		I = i;
		J = j - 1;
				
		if(*Macrof(I, J)==0)//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
		*Macrof(I, J) = 1;
		*Macrof(i, j) = 0;
		*TracerT(I, J) = *TracerT(i, j);
		*TracerT(i, j) = 0;
		}
	 }

	//BOUNDARY SPROUTS We can ask it to be periodical?
	
	 if(I == 0 || I == Xnodes - 1 || J == 0 || J == Ynodes - 1)
	 {
	  *Macrof(I, J) = 0;
		*Macrof(i, j) = 0;
		*TracerT(I, J) = 0;
		*TracerT(i, j) = 0;
	  }
	 
	 }
	 } 
	 }//this is for the if
	 
    }//this is for the loop
  extern Matrix MacrofD;
  
  MacrofD = zeros(Xnodes, Ynodes);
  *MacrofD(0,0) = 1;
  
for(int i = 0; i< Xnodes; i++)
{
for(int j = 0; j< Ynodes; j++)
{
if(*TracerT(i,j)>0&&*Macrof(i,j)>0)
{
*MacrofD(i,j) = 1;
}
else if(*Macrof(i,j)>0)
{
*MacrofD(i,j) =0.5;
}

}
}
  
}
bool VesselStructure::UpdateOxyVWTPressure( void )
{
	extern double _EPS;
	
	extern int Xnodes;
	extern int Ynodes;
	extern Matrix X;
	extern Matrix Y;
	extern double dx, dy;
	
	extern Matrix Phi;
	extern Matrix PhiNecrotic;
	extern Matrix PhiQuiescent;
	extern Matrix OxygenDiffusivity;
	extern Matrix BloodVesselIndicator;
	extern Matrix Oxygen;
	extern Matrix Mobility;
	extern Matrix Pressure;
	extern Matrix Leak;
	extern Matrix Drainage;
	extern Matrix TotalPressure;
	extern Matrix Pressure;
	extern Matrix OutPressure;
	extern double ApoptosisRate;
	extern double TumorNecrosisRateGN;
	extern double TumorProliferationConstant;
	extern char* StringBuffer;
	extern char* LogFile;
	extern double ChMobility;
	extern double ChLength;
	extern double MitosisRate;
	extern double VFCell;
	extern double VFWater;
	extern double Lym;
	extern double LymPressure;
	extern Matrix Original_ECM;
	extern bool TopBloodVessel;
	extern bool IsFirstOxygenUpdate;
	extern Matrix HomPressure;
	extern Matrix InterPressure, Permeability, Conduct;
	extern Matrix CollapseClock;
    extern Matrix TaxisJump;
	UpdateOxygenDiffusivity();//HERE
	
	Matrix PressureSource = zeros(Xnodes,Ynodes);
	Matrix PressureSourceU = zeros(Xnodes,Ynodes);
	
	extern Matrix ECMtaxisCoefficient, ECM;
	
	UpdateECMtaxisCoefficient();
	
	TaxisJump = zeros(Xnodes,Ynodes);
	Leak = zeros(Xnodes,Ynodes);
	extern double FunctionSmoothingLevel;
	Matrix ECMsmoothed = GaussianFilter(X,Y,ECM,FunctionSmoothingLevel*dx); 
	
	
	UpdateMobility();
	
	
	if( Oxygen.TellRows() != Xnodes || Oxygen.TellCols() != Ynodes )
	{ 
		Oxygen = zeros(Xnodes,Ynodes); 
		StartStringBuffer( "Warning: Oxygen previously uninitialized. Building ... \n" );
		cout << StringBuffer << endl;
		MakeLogEntry( LogFile, StringBuffer );
	}
	if( OxygenDiffusivity.TellRows() != Xnodes || OxygenDiffusivity.TellCols() != Ynodes )
	{ 
		OxygenDiffusivity = ones(Xnodes,Ynodes);  
		StartStringBuffer( "Warning: Oxygen diffusivity previously uninitialized. Building ... \n" );
		cout << StringBuffer << endl;
		MakeLogEntry( LogFile, StringBuffer );
	}
	if( Oxygen.TellRows() != Xnodes || Oxygen.TellCols() != Ynodes )
	{
		Oxygen = zeros(Xnodes,Ynodes);
		StartStringBuffer( "Warning: Oxygen diffusivity previously uninitialized. Building ... \n" );
		cout << StringBuffer << endl;
		MakeLogEntry( LogFile, StringBuffer );
	}
	if( PhiNecrotic.TellRows() != Xnodes || PhiNecrotic.TellCols() != Ynodes )
	{
		PhiNecrotic = ones(Xnodes,Ynodes);
		StartStringBuffer( "Warning: PhiN diffusivity previously uninitialized. Building ... \n" );
		cout << StringBuffer << endl;
		MakeLogEntry( LogFile, StringBuffer );
	}
	if( PhiQuiescent.TellRows() != Xnodes || PhiQuiescent.TellCols() != Ynodes )
	{
		PhiQuiescent = ones(Xnodes,Ynodes); 
		StartStringBuffer( "Warning: PhiQ diffusivity previously uninitialized. Building ... \n" );
		cout << StringBuffer << endl;
		MakeLogEntry( LogFile, StringBuffer );
	}
	if( BloodVesselIndicator.TellRows() != Xnodes || BloodVesselIndicator.TellCols() != Ynodes )
	{
		BloodVesselIndicator = zeros(Xnodes,Ynodes);
		StartStringBuffer( "Warning: Blood Vessel Indicator previously uninitialized. Building ... \n" );
		cout << StringBuffer << endl;
		MakeLogEntry( LogFile, StringBuffer );
	}
	
	// test line
	PhiQuiescent = ones(Xnodes,Ynodes);
	
	// end test line
	
	char TopO2conditionType = 'D';
	double (*TopO2BC)(double) = O2_OneFunction;
	if( !TopBloodVessel )
	{
		TopO2conditionType = 'N'; 
		TopO2BC = O2_ZeroFunction;
	}
	
	extern bool BottomBloodVessel;
	char BottomO2conditionType = 'D';
	double (*BottomO2BC)(double) = O2_OneFunction;
	if( !BottomBloodVessel )
	{
		BottomO2conditionType = 'N'; 
		BottomO2BC = O2_ZeroFunction;
	}
	
	
	int MaxIterations = 200000;
	double OTolerance = IntPow(dx,4);//4
	double PTolerance = IntPow(dx,4);//4
	double WTolerance = IntPow(dx,3);//3
	double VTolerance = IntPow(dx,3);//3
	char bctype = 'D'; // 'N';
	
	// bctype = PressureBoundaryConditionType;
	
	double (*PressureBC_left)(double) = PressureBC;
	double (*PressureBC_right)(double) = PressureBC;
	double (*PressureBC_bottom)(double) = PressureBC;
	double (*PressureBC_top)(double) = PressureBC;
	
	bool UseFarFieldRadialDirichletCondition = false;
	if( UseFarFieldRadialDirichletCondition )
	{
		cout << "Using radial far-field tumor pressure for Dirichlet boundary condition ... " << endl;
		CalculateEquivalentCircularTumor();
		bctype = 'D';
		
		PressureBC_left = FarFieldRadialSolution_left;
		PressureBC_right = FarFieldRadialSolution_right;
		PressureBC_bottom = FarFieldRadialSolution_bottom;
		PressureBC_top = FarFieldRadialSolution_top;
	}
	extern double DRG_Effect;
	extern Matrix TracerD;
	extern double TracerMax;
	
	for( int i=1 ; i < Xnodes-1 ; i++ )
	{
		for( int j=1 ; j < Ynodes-1 ; j++ )
		{
			bool Done = false;
			if( *PhiNecrotic(i,j) <= _EPS )
			{
				*PressureSource(i,j) = -TumorNecrosisRateGN; 
				Done = true; 
			}
			if( *PhiQuiescent(i,j) <= _EPS && !Done )
			{ Done = true; }
			if( *Phi(i,j) <= _EPS && !Done )
			{
				*PressureSource(i,j) =  TumorProliferationConstant*( *Oxygen(i,j)*(1-DRG_Effect* *TracerD(i,j)*TracerMax) - ApoptosisRate);
			}
			
			double ECMtaxisXr   = 0.5*( *ECMtaxisCoefficient(i+1,j) + *ECMtaxisCoefficient(i,j) );
			double ECMtaxisXell = 0.5*( *ECMtaxisCoefficient(i-1,j) + *ECMtaxisCoefficient(i,j) );
			double ECMtaxisYr   = 0.5*( *ECMtaxisCoefficient(i,j+1) + *ECMtaxisCoefficient(i,j) );
			double ECMtaxisYell = 0.5*( *ECMtaxisCoefficient(i,j-1) + *ECMtaxisCoefficient(i,j) );
			
			double ECM_taxis_contribution = ( ECMtaxisXr*(*ECMsmoothed(i+1,j)-*ECMsmoothed(i,j)) -
											 ECMtaxisXell*(*ECMsmoothed(i,j)-*ECMsmoothed(i-1,j))  )/square(dx) 
			+ ( ECMtaxisYr*(*ECMsmoothed(i,j+1)-*ECMsmoothed(i,j)) - 
			   ECMtaxisYell*(*ECMsmoothed(i,j)-*ECMsmoothed(i,j-1))  )/square(dy);
			
			*PressureSourceU(i,j) -= ECM_taxis_contribution; 
			
			GeometricDataPacket GDP = AdaptiveGeometricData(X,Y,Phi,*X(i),*Y(j));
			
			*TaxisJump(i,j) = *ECMtaxisCoefficient(i,j)* (  GDP.NormalX*((*ECMsmoothed(i+1,j)-*ECMsmoothed(i-1,j))/(2.0*dx))
														  +GDP.NormalY*((*ECMsmoothed(i,j+1)-*ECMsmoothed(i,j-1))/(2.0*dy)) );
		}
	}
	
	
	WithOxygenVWTGhostFluidMethod_Generic_MatrixForm(X, Y, Phi, Mobility,
													PressureSource, PressureSourceU, PressureBC_left,bctype, PressureBC_right,bctype, 
													PressureBC_bottom,bctype, PressureBC_top,bctype, PressureJumpFunction, PressureNormalJumpFunction,
													PTolerance, O2_ZeroFunction, 'N', O2_ZeroFunction, 'N', BottomO2BC, BottomO2conditionType, TopO2BC, TopO2conditionType, 
													OxygenDiffusionConstantFunction, OxygenWTSourceTimesO2, OxygenWTSourceRemainder, MaxIterations, OTolerance, ImplicitUpdate, UpdateBoundaryPoint,WTolerance,
													AngioFluidPressureUpdate,
													FluidPressureUpdateBoundaryPoint,VTolerance);
	
	
	//ShiftPressureToMatchAverage();	
	
	UpdateNecrosisLevelset();
	
	UpdateQuiescentLevelset();	
	OutPressure = zeros(Xnodes,Ynodes);
	Drainage = zeros(Xnodes,Ynodes);
	TotalPressure = zeros(Xnodes,Ynodes);
    double temp=0;
	double vtemp=0;
	extern double waterpressureA;
	extern double vesselpressureA;
	extern double hompressureA;
	extern Matrix BloodNodePressure;
	extern double KLmax;
	extern double PLmin;
	extern double PLmax;
	waterpressureA = 0;
	vesselpressureA = 0;
	hompressureA = 0;
	for(int i=0; i< Xnodes; i++)
	{
		for(int j=0; j< Ynodes; j++)
		{
			
			*OutPressure(i,j) = *Pressure(i,j) * MitosisRate * ChLength* ChLength / ChMobility;
			
			//cout<<*OutPressure(i,j)<<"  ";
			if(*BloodVesselIndicator(i,j)>0)
			{
				//*Leak(i,j) = 1000* *Conduct(i,j)* *Permeability(i,j)*(*HomPressure(i,j)-VFWater* *InterPressure(i,j)-VFCell * *OutPressure(i,j));
				*Leak(i,j) = 1000* *Conduct(i,j)* *Permeability(i,j)*(*HomPressure(i,j)- *InterPressure(i,j));
				if(*Leak(i,j)<0)
				{
					*Leak(i,j)= 0;	
				}
				if(*CollapseClock(i,j)>0)
				{
					*Leak(i,j)= 0;	
				}
				if((*Phi(i,j)<0) && (*BloodNodePressure(i,j)>0))
				{
					vtemp++;
					vesselpressureA += *BloodNodePressure(i,j);
					hompressureA += *HomPressure(i,j);
				}
				
			}
			
			if(LymPressure< *InterPressure(i,j))
			{		
				
				double KLTemp;
				if(*Pressure(i,j) <=PLmin)
				{
					KLTemp = 1+(KLmax-1)* *Pressure(i,j)/PLmin;
				}
				else if(*Pressure(i,j)>PLmin && *Pressure(i,j)<PLmax)
				{
					KLTemp = KLmax*(1-(*Pressure(i,j)-PLmin)/(PLmax-PLmin));
				}
				else
				{
					KLTemp = 0;	
				}
				
				
				
				*Drainage(i,j) = - 1000* *Original_ECM(i,j) *Lym * KLTemp * (LymPressure- *InterPressure(i,j));
				if(*Drainage(i,j)<0)
				{
					*Drainage(i,j) = 0;	
				}
			}
			else 
			{
				*Drainage(i,j) = 0;	
			}

			
			
				
			
			
			
			
			
			
			*TotalPressure(i,j) = VFCell* *OutPressure(i,j) + VFWater * *InterPressure(i,j);
		    if(*Phi(i,j)<0)
			{
				temp++;
				waterpressureA += *InterPressure(i,j);				
				
			}
			
			
		}
		
		
	}
	waterpressureA /=temp;
	vesselpressureA /=vtemp;
	hompressureA /=temp;
	
	
	/******
	 
	 double v = (pow(5, 0.5) - 0.1)/(pow(5,0.5) - 1);
	 
	 
	 for(int i=0; i< Xnodes; i++)
	 {
	 for(int j=0; j< Ynodes; j++)
	 {
	 double r = pow( pow((j-120)*1.0/200,2) + pow((i-100)*1.0/200, 2) , 0.5);
	 if(r<= 0.1)
	 {
	 *Pressure(i, j) = 1;
	 }
	 else
	 {
	 *Pressure(i, j) =(v - r)*(v - r)*1.0/pow((v-0.1), 2);
	 
	 }
	 // *TAF(i, j) = exp(-pow((j)*1.0/200,2)/0.45); 
	 //cout<<*TAF(i, j) <<endl;
	 
	 }
	 
	 
	 }
	 
	 
	 
	 
	 
	 *****************/ 
	return true;
}
void VesselStructure::WithOxygenVWTGhostFluidMethod_Generic_MatrixForm(Matrix& X, Matrix& Y, Matrix& Phi, 
													 Matrix& Diffusivity, 
													 Matrix& SourceFunction, Matrix& SourceFunctionU, 
													 double (*PLeftBC)(double),   char PLeftBCtype,  
													 double (*PRightBC)(double),  char PRightBCtype,     
													 double (*PBottomBC)(double), char PBottomBCtype,   
													 double (*PTopBC)(double),    char PTopBCtype,  
													 double (*JumpCondition)(double,double), 
													 double (*NormalJumpCondition)(double,double), double PTolerance,
													 /* Matrix& InitialGuess, */ 
													 double (*LeftBC)(double),   char LeftBCtype,  
													 double (*RightBC)(double),  char RightBCtype,     
													 double (*BottomBC)(double), char BottomBCtype,   
													 double (*TopBC)(double),    char TopBCtype,  
													 double (*DiffusionConstant)(double,double,double,int,int), 
													 double (*SourceTimesU)(double,double,double,int,int),
													 double (*SourceRemainder)(double,double,double,int,int),
													 int MaxIterations, double OTolerance ,
													 double ImplicitUpdate(
																		   Matrix& Solution, 
																		   double (*Diffusion)(double,double,double,int,int),
																		   double (*SourceTimesU)(double,double,double,int,int),
																		   double (*SourceRemainder)(double,double,double,int,int), 
																		   int i, int j, 
																		   double x, double y, double dx, double dy ),
/*
 double UpdateBoundaryPoint( Matrix& Solution, 
 double (*LeftBC)(double),   char LeftBCtype,  
 double (*RightBC)(double),  char RightBCtype,     
 double (*BottomBC)(double), char BottomBCtype,   
 double (*TopBC)(double),    char TopBCtype,  
 int i, int j, 
 double x, double y )*/
													 double UpdateBoundaryPoint(Matrix& Solution, 
																				double (*Diffusion)(double,double,double,int,int),
																				double (*SourceTimesU)(double,double,double,int,int),
																				double (*SourceRemainder)(double,double,double,int,int), 
																				int i, int j, 
																				double x, double y, double dx, double dy),double WTolerance,
													 double AngioFluidPressureUpdate( Matrix& Diffusion,  Matrix& BloodNodePressure, Matrix& Pressure, Matrix& Solution, Matrix& CellSource,  int i, int j),
													 double FluidPressureUpdateBoundaryPoint( Matrix& Diffusion,  Matrix& BloodNodePressure, Matrix& Pressure, Matrix& Solution,   int i, int j), double VTolerance
													 
													 )
{
	
	extern Matrix ConductX,ConductY;
	extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
	extern Matrix BloodNodePressure;
	extern Matrix LymIndicator;
	extern Matrix InterPressure;
	extern Matrix Permeability;
	extern Matrix Conduct;
	extern Matrix X,Y;
	extern int Xnodes, Ynodes;
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;//initialize the updating point
	extern double t;
	double Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
	double Pout = RetrieveDoubleParameter( ParameterFile , "Pout");
	extern double Lym;
	extern double LymPressure;
	extern Matrix HomPressure;
	extern double WaterPressureMin;
	extern double PLmin;
	extern double PLmax;
	extern double KLmax;
	PLmin = RetrieveDoubleParameter( ParameterFile , "PLmin");
	PLmax = RetrieveDoubleParameter( ParameterFile , "PLmax");
	KLmax = RetrieveDoubleParameter( ParameterFile , "KLmax");
	extern double Hom,Hom_o;
	Hom_o = RetrieveDoubleParameter( ParameterFile , "Hom_o");
	Hom = RetrieveDoubleParameter( ParameterFile , "Hom");
	Lym = RetrieveDoubleParameter( ParameterFile , "Lym");
	LymPressure = RetrieveDoubleParameter( ParameterFile , "LymPressure");
	
	WaterPressureMin = RetrieveDoubleParameter(ParameterFile, "WaterPressureMin");
	double CondTissue = RetrieveDoubleParameter( ParameterFile , "CondTissue");
	extern Matrix Shuffle;
	extern char SaveDirectory[1025];
	char temp_string[1024];
	extern Matrix CollapseClock;
	extern Matrix FlowX, FlowY;
	
	
	
	Matrix Iteration_Change_Matrix;
	
	extern double dx;
	extern double dy;
	extern bool AdaptionBegin;
	extern double QuiescenceOxygenLevel;
	int i,j;
	
	//Matrix Output = InitialGuess; 
	extern Matrix Oxygen,Pressure, InterPressure, BloodNodePressure;
	extern Matrix Phi, PhiQuiescent, PhiNecrotic;
	extern double TracerMax;
	extern double _EPS;
	extern double TumorProliferationConstant;
	extern double ApoptosisRate;
	extern double DRG_Effect;
	extern Matrix TracerD;
	int Iteration = 0;
	double PError = 2*PTolerance;
	double OError = 2*OTolerance;
	double WError = 2*WTolerance;
	double VError = 2*WTolerance;
	
	double SelectionRatio = 0.25;
	double SelectionOCriterion = OTolerance;
	double SelectionPCriterion = PTolerance; 
	double SelectionWCriterion = WTolerance; 
	double SelectionVCriterion = VTolerance; 
	
	AngioSolverPoint* pHeadSolverPoint = NULL;
	AngioSolverPoint* pCurrentSolverPoint = pHeadSolverPoint;
	
	int MaxSolverPoints = ( X.TellCols() * Y.TellCols() )/4; 
	MaxSolverPoints = 10 * X.TellCols();
	int NumberOfSolverPoints = 0;
	
	bool GiveUp = false;
	int GiveUpThreshold = (int) floor( (0.015*X.TellCols())*(0.015*Y.TellCols()) );
	
	int SweepDirectionI,SweepDirectionJ;
	int StartI,EndI;
	int StartJ,EndJ;
	
	Iteration_Change_Matrix  = zeros( X.TellCols() , Y.TellCols() );
	
	int k=0;
	
	double mean_change = 0.0;
	double change_variance = 9e9;
	extern char* ParameterFile;
	Matrix AdvectConstant = ones(X.TellCols(),Y.TellCols());
	for(int ii=0; ii<X.TellCols(); ii++)
	{
		for(int jj=0; jj<Y.TellCols(); jj++)
		{
			//cout<<*BloodVesselIndicatorX(ii,jj)<<endl;
			*AdvectConstant(ii,jj) = CondTissue;
		}
	}
	double MaxConduct= 1e-25;
	for(int ii=0; ii<X.TellCols(); ii++)
	{
		for(int jj=0; jj<Y.TellCols(); jj++)
		{
			//cout<<*BloodVesselIndicatorX(ii,jj)<<endl;
			if(*ConductX(ii,jj)>MaxConduct)
			{MaxConduct=*ConductX(ii,jj);}
			if(*ConductY(ii,jj)>MaxConduct)
			{MaxConduct=*ConductY(ii,jj);}
		}
	}
	double change_variance_tolerance = 0.000001*PTolerance;
	if( change_variance_tolerance < 1e-14 )
	{ change_variance_tolerance = 1e-14; }
	////////////////////////
	// if(AdaptionBegin==1)
	// {
	//  Pressure = zeros( X.TellCols() , Y.TellCols() );
	// Oxygen = ones( X.TellCols() , Y.TellCols() );
	// }
	///////////////////////
	while( Iteration < MaxIterations && (VError > VTolerance ||WError > WTolerance ||OError > OTolerance || PError > PTolerance && change_variance > change_variance_tolerance))
	{
		
		
		
		
		
		
		
		double VNewValue = 0;
		
		pCurrentVesselPoint = pFirstVesselPoint;
		VError = 0.0;
		//sweep along the chain
		while(pCurrentVesselPoint)
		{
			i=pCurrentVesselPoint->i;
			j=pCurrentVesselPoint->j;
			
			
			if(i==0&&j==12||j==0&&i==12)
			{
				VNewValue = 3750;
			}
			else if(i==0&&j==37||j==0&&i==37)
			{
				VNewValue = 3700;
			}
			else if(i==0&&j==62||j==0&&i==62)
			{
				VNewValue = 3650;
			}
			else if(i==0&&j==87||j==0&&i==87)
			{
				VNewValue = 3600;
			}
			else if(i==0&&j==112||j==0&&i==112)
			{
				VNewValue = 3550;
			}
			else if(i==0&&j==137||j==0&&i==137)
			{
				VNewValue = 3500;
			}
			else if(i==0&&j==162||j==0&&i==162)
			{
				VNewValue = 3450;
			}
			else if(i==0&&j==187||j==0&&i==187)
			{
				VNewValue = 3400;
			}
			else if(i==200&&j==12||j==200&&i==12)
			{
				VNewValue = 3350;
			}
			else if(i==200&&j==37||j==200&&i==37)
			{
				VNewValue = 3300;
			}
			else if(i==200&&j==62||j==200&&i==62)
			{
				VNewValue = 3250;
			}
			else if(i==200&&j==87||j==200&&i==87)
			{
				VNewValue = 3200;
			}
			else if(i==200&&j==112||j==200&&i==112)
			{
				VNewValue = 3150;
			}
			else if(i==200&&j==137||j==200&&i==137)
			{
				VNewValue = 3100;
			}
			else if(i==200&&j==162||j==200&&i==162)
			{
				VNewValue = 3050;
			}
			else if(i==200&&j==187||j==200&&i==187)
			{
				VNewValue = 3000;
			}
			else
			{
				VNewValue =AngioLymSORUpdate( Permeability, InterPressure, BloodNodePressure, ConductX, ConductY, BloodVesselIndicatorX, BloodVesselIndicatorY, i, j, MaxConduct);
				
			}	 
			
			double Vchange = min_abs( VNewValue - *BloodNodePressure(i,j) );
			if( Vchange > VError )
			{ 
				VError = Vchange;
				
				
			}
			
			*BloodNodePressure(i,j) = VNewValue;
			
			pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//Move on
			// cout << "          error     : " << MaxError <<  endl; 
		}
		
		
		
		
	/*	
		
		
		 pCurrentVesselPoint = pFirstVesselPoint;
		while(pCurrentVesselPoint)
		{
			int i = pCurrentVesselPoint->i;
			int j = pCurrentVesselPoint->j;
			
			
			
			//X direction
			if(i == 0)//boundary
			{
				
				
				if(*BloodVesselIndicatorX(i,j)>0.5)
				{
					*FlowX(i, j) = 0;
					
				}
				
			}
			
			else if(i == Xnodes - 1 )//boundary
			{
				
				if(*BloodVesselIndicatorX(i,j)<-0.5)
				{
					*FlowX(i, j) = ( *BloodNodePressure(i-1, j) - *BloodNodePressure(i, j) ) * *ConductX(i, j);
					if(*CollapseClock(i,j)>0)
					{
						*FlowX(i,j)	= 0;
						
					}
					if(*CollapseClock(i-1,j)>0)
					{
						*FlowX(i,j) = 0;	
					}
					
					
				}
			}
			
			else
			{
				
				if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)//when the point is fully connected
				{
					*FlowX(i, j) = ( *BloodNodePressure(i-1, j) - *BloodNodePressure(i, j) ) * *ConductX(i, j);
					if(*CollapseClock(i,j)>0)
					{
						*FlowX(i,j)	= 0;
						
					}
					if(*CollapseClock(i-1,j)>0)
					{
						*FlowX(i,j) = 0;	
					}
					
				}
				
				else if(*BloodVesselIndicatorX(i,j)<-0.5)
				{
					*FlowX(i, j) = ( *BloodNodePressure(i-1, j) - *BloodNodePressure(i, j) ) * *ConductX(i, j);
					if(*CollapseClock(i,j)>0)
					{
						*FlowX(i,j)	= 0;
						
					}
					if(*CollapseClock(i-1,j)>0)
					{
						*FlowX(i,j) = 0;	
					}
					
				}
				
				else if(*BloodVesselIndicatorX(i,j)>0.5)
				{
					*FlowX(i, j) = 0.0;
					
				}
				
				
			}
			
			// Y direction
			
			if(j == 0)//boundary
			{
				
				
				if(*BloodVesselIndicatorY(i,j)>0.5)
				{
					*FlowY(i, j) = 0;
					
				}
				
			}
			
			else if(j == Ynodes - 1 )//boundary
			{
				
				if(*BloodVesselIndicatorY(i,j)<-0.5)
				{
					*FlowY(i, j) = ( *BloodNodePressure(i, j-1) - *BloodNodePressure(i, j) ) * *ConductY(i, j);
					if(*CollapseClock(i,j)>0)
					{
						*FlowY(i,j)	= 0;
						
					}
					if(*CollapseClock(i,j-1)>0)
					{
						*FlowY(i,j) = 0;	
					}
					
				}
				
			}
			
			else
			{
				
				if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)//when the point is fully connected
				{
					*FlowY(i, j) = ( *BloodNodePressure(i, j-1) - *BloodNodePressure(i, j) ) * *ConductY(i, j);
					if(*CollapseClock(i,j)>0)
					{
						*FlowY(i,j)	= 0;
						
					}
					if(*CollapseClock(i,j-1)>0)
					{
						*FlowY(i,j) = 0;	
					}
					
				}
				
				else if(*BloodVesselIndicatorY(i,j)<-0.5)
				{
					*FlowY(i, j) = ( *BloodNodePressure(i, j-1) - *BloodNodePressure(i, j) ) * *ConductY(i, j);
					if(*CollapseClock(i,j)>0)
					{
						*FlowY(i,j)	= 0;
						
					}
					if(*CollapseClock(i,j-1)>0)
					{
						*FlowY(i,j) = 0;	
					}
					
				}
				
				else if(*BloodVesselIndicatorY(i,j)>0.5)
				{
					*FlowY(i, j) = 0;
				}
				
			}
			
			if(*ConductX(i,j)!=0)
			{
				if(min_abs(*FlowX(i,j)/ *ConductX(i,j))<VTolerance)
				{
					*FlowX(i,j) = 0;
				}
			}
			
			if(*ConductY(i,j)!=0)
			{
				if(min_abs(*FlowY(i,j)/ *ConductY(i,j))<VTolerance)
				{
					*FlowY(i,j) = 0;
					
				}
			}
			
			
						
			
			
			
			pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
			
		}
		
		*/
		
		if( Iteration % 500 == 0 )
		{   
			GetVesselPressureAndFlow(VTolerance);
			SteadyStateHematocrit();			
			
		}  
		
		
		mean_change = 0.0;
		double Number_of_nodes = ( X.TellCols()-2.0 )*( Y.TellCols() - 2.0 );
		
		// Set the sweep directions
		if( k % 4 == 0 )
		{ 
			SweepDirectionI = 1;
			StartI = 1;
			EndI = Oxygen.TellRows()-1;
			
			SweepDirectionJ = 1;
			StartJ = 1;
			EndJ = Oxygen.TellCols()-1;
		}
		if( k % 4 == 1 )
		{ 
			SweepDirectionI = -1;
			EndI = 0;
			StartI = Oxygen.TellRows()-2;
			
			SweepDirectionJ = 1;
			StartJ = 1;
			EndJ = Oxygen.TellCols()-1;
		}
		if( k % 4 == 2 )
		{ 
			SweepDirectionI = 1;
			StartI = 1;
			EndI = Oxygen.TellRows()-1;
			
			SweepDirectionJ = -1;
			EndJ = 0;
			StartJ = Oxygen.TellCols()-2;
		}
		if( k % 4 == 3 )
		{ 
			SweepDirectionI = -1;
			EndI = 0;
			StartI = Oxygen.TellRows()-2;
			
			SweepDirectionJ = -1;
			EndJ = 0;
			StartJ = Oxygen.TellCols()-2;
		}
		
		// update the boundary points
		
		for( i=0 ; i < X.TellCols() ; i++ )
		{
			j=0;
			
			
			double  ONewValue = UpdateBoundaryPoint( Oxygen, DiffusionConstant, SourceTimesU, SourceRemainder, i,j,*X(i),*Y(j),dx,dy );
			
			double Ochange = fabs( ONewValue - *Oxygen(i,j) );
			if( Ochange > OError )
			{ OError = Ochange; }
			*Oxygen(i,j) = ONewValue; 
			
			
			
			
			*Pressure(i,j) = gfmUpdateBoundaryPoint( X,Y,Pressure,PLeftBC,PLeftBCtype,
													PRightBC,PRightBCtype,PBottomBC,PBottomBCtype,
													PTopBC,PTopBCtype,i,j); 
			*InterPressure(i,j) = FluidPressureUpdateBoundaryPoint( AdvectConstant,  BloodNodePressure, Pressure, InterPressure, i, j);
			
			
			
			
			j=Y.TellCols()-1;
			ONewValue = UpdateBoundaryPoint( Oxygen, DiffusionConstant, SourceTimesU, SourceRemainder, i,j,*X(i),*Y(j),dx,dy );
			
			
			Ochange = fabs( ONewValue - *Oxygen(i,j) );
			if( Ochange > OError )
			{ OError = Ochange; }
			*Oxygen(i,j) = ONewValue; 
			
			
			
			*Pressure(i,j) = gfmUpdateBoundaryPoint( X,Y,Pressure,PLeftBC,PLeftBCtype,
													PRightBC,PRightBCtype,PBottomBC,PBottomBCtype,
													PTopBC,PTopBCtype,i,j); 
			*InterPressure(i,j) = FluidPressureUpdateBoundaryPoint( AdvectConstant,  BloodNodePressure, Pressure, InterPressure, i, j);		
			// cout<<*InterPressure(i,j)<<endl;
			//getchar();
			
			
		}									   
		for( j = 1 ; j < Y.TellCols()-1 ; j++ )
		{
			i=0;
			/*
			 double ONewValue = UpdateBoundaryPoint(Oxygen,
			 LeftBC,LeftBCtype, 
			 RightBC,RightBCtype,
			 BottomBC,BottomBCtype,
			 TopBC,TopBCtype,
			 i,j,*X(i),*Y(j));
			 */
			double ONewValue = UpdateBoundaryPoint( Oxygen, DiffusionConstant, SourceTimesU, SourceRemainder, i,j,*X(i),*Y(j),dx,dy );
			double Ochange = fabs( ONewValue - *Oxygen(i,j) );
			if( Ochange > OError )
			{ OError = Ochange; }
			*Oxygen(i,j) = ONewValue; 
			
			
			*Pressure(i,j) = gfmUpdateBoundaryPoint( X,Y,Pressure,PLeftBC,PLeftBCtype,
													PRightBC,PRightBCtype,PBottomBC,PBottomBCtype,
													PTopBC,PTopBCtype,i,j); 
			*InterPressure(i,j) = FluidPressureUpdateBoundaryPoint( AdvectConstant,  BloodNodePressure,Pressure, InterPressure, i, j);
			
			
			
			i=X.TellCols()-1;
			
			
			ONewValue = UpdateBoundaryPoint( Oxygen, DiffusionConstant, SourceTimesU, SourceRemainder, i,j,*X(i),*Y(j),dx,dy );
			
			
			Ochange = fabs( ONewValue - *Oxygen(i,j) );
			if( Ochange > OError )
			{ OError = Ochange; }
			*Oxygen(i,j) = ONewValue; 
			
			
			*Pressure(i,j) = gfmUpdateBoundaryPoint( X,Y,Pressure,PLeftBC,PLeftBCtype,
													PRightBC,PRightBCtype,PBottomBC,PBottomBCtype,
													PTopBC,PTopBCtype,i,j); 
			*InterPressure(i,j) = FluidPressureUpdateBoundaryPoint( AdvectConstant,  BloodNodePressure, Pressure, InterPressure, i, j);
			
		}			
		
		// sweep and note the maximum
		
		PError = 0.0;
		OError = 0.0;
		WError = 0.0;
		i = StartI;
		while( i != EndI )
		{
			j = StartJ;
			while( j != EndJ )
			{
				double ONewValue = ImplicitUpdate( Oxygen, 
												  DiffusionConstant, SourceTimesU, SourceRemainder, 
												  i,j,*X(i),*Y(j),dx,dy );
				
				double Ochange = fabs( ONewValue - *Oxygen(i,j) );
				if( Ochange > OError )
				{ OError = Ochange; } 
				*Oxygen(i,j) = ONewValue; 
				
				
				
				if( *Phi(i,j) <= _EPS && *PhiNecrotic(i,j)>_EPS && *Oxygen(i,j)>QuiescenceOxygenLevel )
				{
					*SourceFunction(i,j) =  TumorProliferationConstant*( *Oxygen(i,j)*(1-DRG_Effect* *TracerD(i,j)*TracerMax) - ApoptosisRate);
					
				}
				
				if( *Phi(i,j) <= _EPS && *PhiNecrotic(i,j)>_EPS && *Oxygen(i,j)<=QuiescenceOxygenLevel )
				{
					*SourceFunction(i,j) =  0.0;
				}
				
				
				double PNewValue = gfmImplicitUpdateContinuousD_MatrixForm(X,Y,Phi,Pressure, 
																		   Diffusivity,
																		   SourceFunction,SourceFunctionU,
																		   JumpCondition,NormalJumpCondition,i,j,dx,dy);
				
				double Pchange = fabs( PNewValue - *Pressure(i,j) );
				*Iteration_Change_Matrix(i,j) = Pchange; 
				mean_change += Pchange;
				
				if( Pchange > PError )
				{ PError = Pchange; }
				*Pressure(i,j) = PNewValue;
				
				
				
				double WNewValue = AngioFluidPressureUpdate( AdvectConstant,  BloodNodePressure, Pressure,  InterPressure, SourceFunction,  i, j);
				
				double Wchange = fabs( WNewValue - *InterPressure(i,j) );
				if( Wchange > WError )
				{ WError = Wchange; } 
				*InterPressure(i,j) = WNewValue; 
				
				
				
				
				j += SweepDirectionJ;
			}
			i += SweepDirectionI;
		}
		Iteration++;
		
		mean_change /= Number_of_nodes;
		change_variance = 0.0;
		for( int ii=1 ; ii < Iteration_Change_Matrix.TellRows()-1 ; ii++ )
		{
			for( int jj=1 ; jj < Iteration_Change_Matrix.TellCols()-1 ; jj++ )
			{
				change_variance += square( *Iteration_Change_Matrix(ii,jj) - mean_change );
			}
		}  
		change_variance /= Number_of_nodes;
		//  cout << mean_change << " +/- " << change_variance << endl;
		
		// create the selection criterion 
		SelectionOCriterion = SelectionRatio*OError;
		SelectionPCriterion = SelectionRatio*PError;
		SelectionWCriterion = SelectionRatio*WError;
		pCurrentSolverPoint = pHeadSolverPoint;
		NumberOfSolverPoints = 0;
		
		// sweep and create the list  
		
		i = StartI;  
		while( i != EndI )
		{
			j = StartJ;
			while( j != EndJ )
			{
				
				double ONewValue = ImplicitUpdate( Oxygen, 
												  DiffusionConstant, SourceTimesU, SourceRemainder, 
												  i,j,*X(i),*Y(j),dx,dy );
				double Ochange = fabs( ONewValue - *Oxygen(i,j) );
				if( Ochange > OError )
				{ OError = Ochange; } 
				*Oxygen(i,j) = ONewValue; 
				
				if( *Phi(i,j) <= _EPS && *PhiNecrotic(i,j)>_EPS && *Oxygen(i,j)>QuiescenceOxygenLevel )
				{
					*SourceFunction(i,j) =  TumorProliferationConstant*( *Oxygen(i,j)*(1-DRG_Effect* *TracerD(i,j)*TracerMax) - ApoptosisRate);
				}
				
				if( *Phi(i,j) <= _EPS && *PhiNecrotic(i,j)>_EPS && *Oxygen(i,j)<=QuiescenceOxygenLevel )
				{
					*SourceFunction(i,j) =  0.0;
				}
				double PNewValue = gfmImplicitUpdateContinuousD_MatrixForm(X,Y,Phi,Pressure, 
																		   Diffusivity,
																		   SourceFunction,SourceFunctionU,
																		   JumpCondition,NormalJumpCondition,i,j,dx,dy);
				
				double Pchange = fabs( PNewValue - *Pressure(i,j) );
				*Pressure(i,j) = PNewValue;
				double WNewValue = AngioFluidPressureUpdate( AdvectConstant,  BloodNodePressure, Pressure, InterPressure, SourceFunction,  i, j);
				
				double Wchange = fabs( WNewValue - *InterPressure(i,j) );
				if( Wchange > WError )
				{ WError = Wchange; } 
				*InterPressure(i,j) = WNewValue; 
				/** 
				 if( (Pchange > SelectionPCriterion || Ochange > SelectionOCriterion)&& NumberOfSolverPoints < MaxSolverPoints  )
				 {
				 AngioSolverPoint* pNewSolverPoint;
				 pNewSolverPoint = new AngioSolverPoint; 
				 pNewSolverPoint->i = i;
				 pNewSolverPoint->j = j;
				 NumberOfSolverPoints++;
				 if( pCurrentSolverPoint )
				 { pCurrentSolverPoint->pNextSolverPoint = pNewSolverPoint; }
				 else
				 { pHeadSolverPoint = pNewSolverPoint; }
				 pCurrentSolverPoint = pNewSolverPoint; 
				 }
				 
				 **/
				j += SweepDirectionJ;
			}
			i += SweepDirectionI;
		}
		
		Iteration++;
		
		// sweep over the list 
		
		int ListIteration = 0;
		double TempOError = 2.0*SelectionOCriterion;
		double TempPError = 2.0*SelectionPCriterion; 
		double TempWError = 2.0*SelectionWCriterion;
		/*********
		 int MaxInternalIterations = 2;
		 while( ListIteration < MaxInternalIterations && 
         ( TempPError > 0.99*SelectionPCriterion && 
		 TempPError < 1 ||TempOError > 0.99*SelectionOCriterion && 
		 TempOError < 1 ))
		 {
		 pCurrentSolverPoint = pHeadSolverPoint;
		 TempOError = 0.0;
		 TempPError = 0.0;
		 while( pCurrentSolverPoint )
		 {
		 i = pCurrentSolverPoint->i;
		 j = pCurrentSolverPoint->j;
		 
		 double ONewValue = ImplicitUpdate( Oxygen, 
		 DiffusionConstant, SourceTimesU, SourceRemainder, 
		 i,j,*X(i),*Y(j),dx,dy );
		 *Oxygen(i,j) = ONewValue; 
		 
		 double Ochange = fabs( ONewValue - *Oxygen(i,j) );
		 if( Ochange > TempOError )
		 { TempOError = Ochange; } 
		 *Oxygen(i,j) = ONewValue; 
		 
		 if( *Phi(i,j) <= _EPS && *PhiNecrotic(i,j)>_EPS && *Oxygen(i,j)>QuiescenceOxygenLevel )
		 {
		 *SourceFunction(i,j) =  TumorProliferationConstant*( *Oxygen(i,j) - ApoptosisRate);
		 }
		 
		 if( *Phi(i,j) <= _EPS && *PhiNecrotic(i,j)>_EPS && *Oxygen(i,j)<=QuiescenceOxygenLevel )
		 {
		 *SourceFunction(i,j) =  0.0;
		 }
		 
		 double PNewValue = gfmImplicitUpdateContinuousD_MatrixForm(X,Y,Phi,Pressure, 
		 Diffusivity,
		 SourceFunction,SourceFunctionU,
		 JumpCondition,NormalJumpCondition,i,j,dx,dy);
		 
		 double Pchange = fabs( PNewValue - *Pressure(i,j) );
		 if( Pchange > TempPError )
		 { TempPError = Pchange; }
		 *Pressure(i,j) = PNewValue;   
		 
		 pCurrentSolverPoint = pCurrentSolverPoint->pNextSolverPoint;
		 }
		 ListIteration++;
		 }
		 ***/
		// delete the list
		
		pCurrentSolverPoint = pHeadSolverPoint;  
		while( pHeadSolverPoint )
		{
			pCurrentSolverPoint = pHeadSolverPoint->pNextSolverPoint;
			delete pHeadSolverPoint;
			pHeadSolverPoint = pCurrentSolverPoint;
		}
		
		if( Iteration % 1000 == 0 )
		{   
			
			cout << Iteration << " : " << VError << endl;
			cout << Iteration << " : " << WError << endl;
			cout << Iteration << " : " << PError << endl;
			cout << Iteration << " : " << OError << endl;
			
		}  
		
		k++;
	}
	cout << "summary:: iterations: " << Iteration << endl
	<< "          WError     : " << WError << endl << endl;
	if( GiveUp )
	{
		cout << "Gave up because adaptivity was being focused on less than 0.0225% of nodes!" << endl
		<< "(" << NumberOfSolverPoints << " nodes)" << endl << endl; 
	} 
	
} 



